Angular ui-router无法解析命名依赖项

时间:2022-03-20 11:36:18

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

我最近从ui-router 0.0.1迁移到0.2.0。自迁移以来,ui-router无法解析需要注入视图控制器的命名依赖项。这是示例代码,它与ver 0.0.1一起工作正常但在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

i18nService是一个返回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

使用v0.2.0时,我收到错误“Unknown provider:i18nProvider < - i18n”

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

这里是plunker:http://plnkr.co/edit/johqGn1CgefDVKGzIt6q?p = preview

1 个解决方案

#1


2  

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).

我不相信它在任何当前发布的版本中,但您可以从github获取最新版本或在您的js文件中自行更改。它只是在一行中更改键值(您可以在github提交中看到它)。

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).

然后将i18nService注入您的控制器而不是i18n。这有点像黑客,但它确实有效(它注入已解决的服务而不是承诺)。

#1


2  

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).

我不相信它在任何当前发布的版本中,但您可以从github获取最新版本或在您的js文件中自行更改。它只是在一行中更改键值(您可以在github提交中看到它)。

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).

然后将i18nService注入您的控制器而不是i18n。这有点像黑客,但它确实有效(它注入已解决的服务而不是承诺)。