私の角度コントローラでは、変数を使用して関数を呼び出す必要があります。問題は、サービス名と関数名の両方が動的に定義されているため、どちらも変数になります。 Angular $インジェクタを使用してAngularサービス名を問題なく取得しています。ただし、関数呼び出しが機能しません:サービスと関数名の両方の変数を使用してJavaScript関数を呼び出す
// serviceTypeName and functionTypeName are from the view
// vm.initiate() is invoked by ng-click
//
// $injector is injected to the controller
vm.initiate = function(serviceTypeName, functionTypeName) {
// e.g., if serviceTypeName is 'abcService' and the functionTypeName is 'foo'
// then abcService.foo() is what I hope to get.
// abcService.foo() is defined in the abcService.
var resourceService = $injector.get(serviceTypeName);
// the following works:
var data = resourceService.foo();
// the following statements do not work
var result = resourceService.functionTypeName(); // does not work
var result1 = resourceService.eval(functionTypeName)(); // does not work either
}
AngularJS 1.5.xを使用することはできますか?もしそうなら、誰かがこの問題を解決するために私を助けることができますか?
resourceService [functionTypeName](); – epascarello
これは魅力的に機能しました。ありがとう。 – redixint