角UI路由器无法解析指定的依赖性 [英] Angular ui-router fails to resolve named dependencies

查看:196
本文介绍了角UI路由器无法解析指定的依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从UI路由器0.0.1迁移到0.2.0。由于迁移,UI路由器无法解析指定的依赖性,需要被注入到一个视图的控制器。这里的样本code的正常工作与版本0.0.1版本,但失败0.2.0在

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是一个简单的服务,回报的承诺

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;
}]);

我得到的错误未知提供商:i18nProvider< - 国际化使用v0.2.0时

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

如果我改变决心config来:

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?

这里的plunker: http://plnkr.co/edit/johqGn1CgefDVKGzIt6q?p = preVIEW

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

推荐答案

这是在上月修正了一个错误:

This is a bug that was fixed last month:

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

我不相信这是任何目前发布的版本,但你可以要么得到从GitHub最新的,或在您的js文件自己的变化。它只是在一行改变关键值(你可以看到它在github上提交)。

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

一个workarround是只是不是现在改名字....做

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

resolve :{
  i18nService: '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).

这篇关于角UI路由器无法解析指定的依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆