Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular-JS strict-DI doesn't like injecting resolved results from $routeProvider

Tags:

angularjs

I hit some problems minifying my Angular code so I turned on ng-strict-di

One problem seems to reside in the way I resolve a promise on a route in my app.js config

.when('/:userId', {
           templateUrl: 'views/main.html', 
           controller: 'MyCtrl',  
           resolve : {
               myDependency : function(Cache, Model, $route){ 
                  return Cache.getCached( $route.current.params.userId); 
               } 
           }
      })

Then I inject this resolved promise into the MyCtrl controller

angular.module('myApp') 
    .controller('MyCtrl',[ 'myDependency',  '$scope', '$rootScope', '$timeout', function (myDependency, $scope, $rootScope, $timeout) { 
 etc... 

However I get an error from Angular

[Error] Error: [$injector:strictdi] myDependency is not using explicit annotation and cannot be invoked in strict mode 

The problem appears to be traceable to the resolve definition in app.js because I can change the name of 'myDependency' there in the resolve and the error message uses the name from there rather than the name of the dependency in myCtrl. And I am explicitly listing the name of the dependency in the myCtrl controller. The app works, but I cannot minify this code because of the problem with this error.

like image 737
Rocket Garden Avatar asked Apr 01 '15 13:04

Rocket Garden


1 Answers

Follow strict-di for resolve as well. Hope this works!

resolve : {
           myDependency : ['Cache', 'Model', '$route', function(Cache, Model, $route){ 
               return Cache.getCached( $route.current.params.userId); 
           } 
     ]}
like image 120
msapkal Avatar answered Nov 15 '22 04:11

msapkal