2017-12-20 14 views
1

を構造化代入配列を使用して、角度$インジェクタから複数の値を読む:私はこの持っ

beforeEach(inject(function ($injector, CategoryService) { 

    CategoryService.create = function() { 
    return Promise.resolve({}); 
    }; 

    const $compile = $injector.get('$compile'); 
    const $rootScope = $injector.get('$rootScope'); 
    const $templateCache = $injector.get('$templateCache'); 

})); 

をこのような何かを行う方法がある場合、私は疑問に思って:

ドキュメントを見て
beforeEach(inject(function ($injector, CategoryService) { 

    CategoryService.create = function() { 
    return Promise.resolve({}); 
    }; 

    const [$compile, $rootScope, $templateCache] = $injector.get('$compile','$rootScope','$templateCache'); 

})); 
+1

方法のconst [$コンパイル、$ rootScope、$ templateCache] = [ 'について'$ comp'、 '$ rootScope'、 '$ templateCache']。map(svc => $ injector.get(svc)) ' –

+0

これはうまくいきました。 –

答えて

0

https://docs.angularjs.org/api/auto/service/$injector

そして私はまた、$インジェクタのメソッドをログに記録:

'$injector =>', Object{invoke: function invoke(fn, self, locals, serviceName) { ... }, instantiate: function instantiate(Type, locals, serviceName) { ... }, get: function getService(serviceName, caller) { ... }, annotate: function (fn) { ... }, has: function (name) { ... }, strictDi: false} 

そうする方法がないように見えます。おそらく、配列の破壊はまだほとんどのブラウザで蒸散が必要なためでしょうか?

const myInjector = $injector => { 
    return function(){ 
     return Array.from(arguments).map(function(a){ 
     return $injector.get(a); 
     }) 
    } 
    }; 

などのようにそれを使用します:

だから、あなたはこれを行うには、ヘルパー関数を作成することができます

const inj = myInjector($injector); 
const [AcqService, RolesService] = inj('AcqService', 'RolesService'); 
関連する問題