angularjs – Angular-JS strict-DI不喜欢从$routeProvider注入解析结果

前端之家收集整理的这篇文章主要介绍了angularjs – Angular-JS strict-DI不喜欢从$routeProvider注入解析结果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在缩小我的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控制器中依赖项的名称.该应用程序工作,但由于此错误的问题,我无法缩小此代码.

解决方法

遵循strict-di来解决问题.希望这个有效!

resolve : {
           myDependency : ['Cache','Model','$route',function(Cache,$route){ 
               return Cache.getCached( $route.current.params.userId); 
           } 
     ]}

猜你在找的Angularjs相关文章