【发布时间】:2015-04-01 13:51:56
【问题描述】:
我在缩小 Angular 代码时遇到了一些问题,所以我打开了 ng-strict-di
一个问题似乎在于我在 app.js 配置中解决路由承诺的方式
.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...
但是我从 Angular 得到一个错误
[Error] Error: [$injector:strictdi] myDependency is not using explicit annotation and cannot be invoked in strict mode
问题似乎可以追溯到 app.js 中的解析定义,因为我可以在解析中更改“myDependency”的名称,并且错误消息使用那里的名称而不是 myCtrl 中的依赖项名称。我在 myCtrl 控制器中明确列出了依赖项的名称。该应用程序可以运行,但由于此错误的问题,我无法缩小此代码。
【问题讨论】:
-
Mahesh 是对的(如下),但我发现我的解决方案是运行 ng-annotate 任务以删除并重新编写依赖项。 ng-strict-di 在突出问题区域方面非常有用。
标签: angularjs