Angularjs 角度JS严格DI不';我不喜欢从$routeProvider注入解析结果
我在缩小角度代码时遇到了一些问题,所以我打开了ng strict di 一个问题似乎存在于我在app.js配置中解析路由承诺的方式中Angularjs 角度JS严格DI不';我不喜欢从$routeProvider注入解析结果,angularjs,Angularjs,我在缩小角度代码时遇到了一些问题,所以我打开了ng strict di 一个问题似乎存在于我在app.js配置中解析路由承诺的方式中 .when('/:userId', { templateUrl: 'views/main.html', controller: 'MyCtrl', resolve : { myDependency : function(Cache, Model, $route){
.when('/:userId', {
templateUrl: 'views/main.html',
controller: 'MyCtrl',
resolve : {
myDependency : function(Cache, Model, $route){
return Cache.getCached( $route.current.params.userId);
}
}
})
然后我将这个已解决的承诺注入MyCtrl控制器
angular.module('myApp')
.controller('MyCtrl',[ 'myDependency', '$scope', '$rootScope', '$timeout', function (myDependency, $scope, $rootScope, $timeout) {
etc...
但是我从角度上得到了一个错误
[Error] Error: [$injector:strictdi] myDependency is not using explicit annotation and cannot be invoked in strict mode
这个问题似乎可以追溯到app.js中的resolve定义,因为我可以在resolve中更改“myDependency”的名称,并且错误消息使用了其中的名称,而不是myCtrl中的依赖项名称。我在myCtrl控制器中明确列出了依赖项的名称。该应用程序可以工作,但由于此错误的问题,我无法缩小此代码。请遵循严格的di进行解决。希望这能奏效
resolve : {
myDependency : ['Cache', 'Model', '$route', function(Cache, Model, $route){
return Cache.getCached( $route.current.params.userId);
}
]}
我发现了同样的问题,@Mahesh Sapkal解决方案是正确的 但如果仔细看一下,我的问题是
ng annotate
没有正确检测到必须对函数进行注释。所以我添加了/@ngInject/comment,现在就可以了
app.config(/*@ngInject*/function ($routeProvider) {
$routeProvider
.when('/tables', {
template: templateList,
controller: 'TableListController',
resolve: {
initial: /*@ngInject*/function (tableListControllerInitial) {
return tableListControllerInitial();
}
}
})
我想知道这样做是否可行,但随后我得到一个错误,即我的“缓存”和“模型”依赖项(它们是我的自定义依赖项)不是已知的提供者。出于同样的原因,我不能将它们注入到配置中。谢谢你的建议,因为我正在抓挠我的头。我在我的构建中添加了一个gulp-ng注释任务,现在事情已经解决了(我使用remove和add重写了我所有的注释)。奇怪的是,resolve子句已经按照您的建议进行了转换,现在可以工作了。再次感谢您的建议。Mahesh是对的(如下),但我发现我的解决方案是运行ng annotate任务来删除并重新编写依赖项。ng strict di在突出问题领域方面是非常宝贵的。