Angular ui-router fails to resolve named dependencies

淺唱寂寞╮ 提交于 2019-12-05 10:00:29

问题


I recently migrated from ui-router 0.0.1 to 0.2.0. Since the migration, ui-router fails to resolve named dependencies that needs to be injected into a view's controller. Here's the sample code which works fine with ver 0.0.1 but fails in ver 0.2.0

angular.module( 'sample.test', [
 'ui.router',
 'i18nService'
])

.config(function config($stateProvider) {
    $stateProvider.state( 'mystate', {
      url: '/mystate',
      resolve: {i18n: 'i18nService'},
      views: {
        'main': {
          controller: 'MyCtrl',
          templateUrl: 'templates/my.tpl.html'
        }
      }
    });
})

.controller('MyCtrl', ['i18n', function(i18n) {
   // fails to resolve i18n
}]);

i18nService is a simple service that return a promise

angular.module('i18nService', [])
.factory('i18nService', ['$http', '$q', function($http, $q) {
  var deferred = $q.defer();
  $http.get('..').then(..);

  return deferred.promise;
}]);

I get the error "Unknown provider: i18nProvider <- i18n" when using v0.2.0

If i change the resolve config to:

      resolve: {
        i18n: function(i18nService) {
          return i18nService
        }
      },

everything works fine. Is this an expected behaviour, or am I missing some configuration?

Here's the plunker: http://plnkr.co/edit/johqGn1CgefDVKGzIt6q?p=preview


回答1:


This is a bug that was fixed last month:

https://github.com/angular-ui/ui-router/commit/4cdadcf46875698aee6c3684cc32f2a0ce553c45

I don't believe it's in any currently released version, but you could either get the latest from github or make the change yourself in your js file. It's simply changing key to value in that one line (you can see it in the github commit).

A workarround is to just not change the name for now.... do

resolve :{
  i18nService: 'i18nService'
}

Then inject i18nService to your controller instead of i18n. It's a bit of a hack, but it does work (it injects the resolved service not the promise).



来源:https://stackoverflow.com/questions/19733119/angular-ui-router-fails-to-resolve-named-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!