Angular ui-router 解析中的动态依赖注入

Dynamic dependency injection in Angular ui-router resolve

有什么方法可以在 ui-router resolve 函数中进行依赖注入吗?

我正在努力实现这样的目标:

resolve : {
    anArray : function(/* no dependencies yet */) {
        var currency = 'eur';

        //here I'd like to make a dependency injection
        var currencyDetail = /*injected dependency*/.get();
        return currencyDetail;
    }
}

在最初的项目中,我在 currency 处有一个大数组和一长串服务,例如 eurdatausddata 等,其中许多并不总是用于解析 anArray.

尝试了 $injector.get(currency + 'data') 但没有成功。

您可以使用通常的数组表示法:

resolve: {
  anArray: ['dependency', function(dependency) {
    var currency = 'eur';

     //here I'd like to make a dependency injection
     dependency.doSomething();

    var currencyDetail = /*injected dependency*/.get();
    return currencyDetail;
  }]
}

您可以让 $injector 注入解析函数并请求依赖它

resolve : {
    anArray : ['$injector', function($injector) {
        var currency = 'eur';

        //here I'd like to make a dependency injection
        var currencyDetail = $injector.get(dependencyName);
        return currencyDetail;
    }]
}