Inline Array Annotation DI 与 AngularJS 中的 $routeProvider 不兼容吗?
Isn't Inline Array Annotation DI compatible with $routeProvider in AngularJS?
我的代码是这样的
angular
.module('main', ['ngRoute'])
.config(['$routeProvider',
($routeProvider) ->
$routeProvider
.when '/',
templateUrl: 'homePage/homePage.html'
controller: 'MainCtrl'
])
angular.module('main').controller('MainCtrl',
['$scope' , ($scope) ->
$scope.test = {}])
浏览器会报错Error: [ng:areq] Argument 'MainCtrl' is not a function, got Object
。
但是如果我不使用MainCtrl
中的内联数组依赖注入,那么重写如下:
angular.module('main').controller('MainCtrl',
($scope) ->
$scope.test = {})
然后一切正常。有人对此有想法吗?谢谢!
由于错误消息非常清楚,问题不在于 $routeProvider
,您可能需要重组它们。另请注意,配置块必须具有功能。
首先创建模块,然后注册控制器和配置:
angular
.module('main', ['ngRoute']);
然后使用它或串联,即
angular.module("main", ["ngRoute"]).controller("MainCtrl", [
"$scope"
($scope) ->
return $scope.test = {}
]).config [
"$routeProvider"
($routeProvider) -> //Check this
return $routeProvider.when("/",
templateUrl: "homePage/homePage.html"
controller: "MainCtrl"
)
]
否则,根据您拥有的脚本的顺序,您正在尝试在应用 main
上创建一个控制器,甚至在它存在之前。
另请注意,您还需要包含 angular-router 脚本。
我的代码是这样的
angular
.module('main', ['ngRoute'])
.config(['$routeProvider',
($routeProvider) ->
$routeProvider
.when '/',
templateUrl: 'homePage/homePage.html'
controller: 'MainCtrl'
])
angular.module('main').controller('MainCtrl',
['$scope' , ($scope) ->
$scope.test = {}])
浏览器会报错Error: [ng:areq] Argument 'MainCtrl' is not a function, got Object
。
但是如果我不使用MainCtrl
中的内联数组依赖注入,那么重写如下:
angular.module('main').controller('MainCtrl',
($scope) ->
$scope.test = {})
然后一切正常。有人对此有想法吗?谢谢!
由于错误消息非常清楚,问题不在于 $routeProvider
,您可能需要重组它们。另请注意,配置块必须具有功能。
首先创建模块,然后注册控制器和配置:
angular
.module('main', ['ngRoute']);
然后使用它或串联,即
angular.module("main", ["ngRoute"]).controller("MainCtrl", [
"$scope"
($scope) ->
return $scope.test = {}
]).config [
"$routeProvider"
($routeProvider) -> //Check this
return $routeProvider.when("/",
templateUrl: "homePage/homePage.html"
controller: "MainCtrl"
)
]
否则,根据您拥有的脚本的顺序,您正在尝试在应用 main
上创建一个控制器,甚至在它存在之前。
另请注意,您还需要包含 angular-router 脚本。