所以,按照最佳实践,我开始使用ng-strict-di。它到目前为止运行良好,但我使用ui-router
遇到了以下问题// nested list with custom controller
.state('dashboard.list', {
url: '/list',
templateUrl: 'partials/dashboard-list.html',
controller: function($scope) {
$scope.dogs = ['Bernese', 'Husky', 'Goldendoodle'];
}
})
这会导致对barf的角度为"错误:错误:strictdi 需要明确的注释"错误。
我知道我应该使用内联括号表示法或$ inject,但显然不能按原样将它放在此代码中。
我在想我可以在脚本的另一部分声明控制器,使用$ inject,然后在代码中引用它?
function GoodController1($scope) {
}
GoodController1.$inject = ["$scope"];
然后
// nested list with custom controller
.state('dashboard.list', {
url: '/list',
templateUrl: 'partials/dashboard-list.html',
controller: GoodController1
})
这会有用吗?这种方法有什么问题吗?
答案 0 :(得分:2)
采用这种方法没有问题。我正在使用typescript,而controlelr类的生成语法几乎与你的相同。
...
// the contoller funciton to be instantiated
// by angular using new
var GoodController1 = function($scope){
$scope.title = "good title";
};
// set of dependencies
// (in typescript that would be a static property)
GoodController1.$inject = ["$scope"];
// before angular 2.0, this is the must
// we still have to register controller in the module
app
.controller('GoodController1', GoodController1)
...
以后的状态:
.state('good', {
url: "/good",
templateUrl: 'tpl.html',
controller: "GoodController1",
})
检查here