Angular ui-router 无法解析命名依赖项 [英] Angular ui-router fails to resolve named dependencies
问题描述
我最近从 ui-router 0.0.1 迁移到 0.2.0.自迁移以来,ui-router 无法解析需要注入视图控制器的命名依赖项.这是示例代码,它在 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;
}]);
我在使用 v0.2.0 时收到错误Unknown provider: i18nProvider <- i18n"
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?
这是 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/提交/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).
一个解决方法是暂时不更改名称....做
A workarround is to just not change the name for now.... do
resolve :{
i18nService: 'i18nService'
}
然后将 i18nService
注入您的控制器而不是 i18n.这有点像 hack,但它确实有效(它注入了已解析的服务,而不是承诺).
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).
这篇关于Angular ui-router 无法解析命名依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!