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

时间:2015-04-01 13:51:57

标签: angularjs

我在缩小Angular代码时遇到了一些问题,因此我启用了ng-strict-di

我在app.js config

中解决路线上的承诺的方式似乎存在一个问题
.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控制器中依赖项的名称。该应用程序可以工作,但由于此错误的问题,我无法缩小此代码。

2 个答案:

答案 0 :(得分:5)

遵循严格的解决方案。希望这个有效!

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

答案 1 :(得分:0)

我遇到了同样的问题,@ 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();
                }
            }
        })
相关问题