我在缩小 Angular 代码时遇到了一些问题,所以我打开了 ng-strict-di
一个问题似乎在于我在 app.js 配置中解决路由 promise 的方式
.when('/:userId', {
templateUrl: 'views/main.html',
controller: 'MyCtrl',
resolve : {
myDependency : function(Cache, Model, $route){
return Cache.getCached( $route.current.params.userId);
}
}
})
然后我将这个已解决的 promise 注入(inject) MyCtrl Controller
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 Controller 中明确列出了依赖项的名称。该应用程序有效,但由于此错误的问题,我无法缩小此代码。
最佳答案
也遵循 strict-di 来解决。希望这有效!
resolve : {
myDependency : ['Cache', 'Model', '$route', function(Cache, Model, $route){
return Cache.getCached( $route.current.params.userId);
}
]}
关于angularjs - Angular-JS strict-DI 不喜欢从 $routeProvider 注入(inject)解析的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29392618/