strwoc
strwoc

Reputation: 531

Call serviceName.functionName dynamically Angular

I call a function

getData('serviceName', 'functionName')

That function looks like

function getData(service, functionName){
    service.functionName(request, $root.thing).then()
}

But I always get service.function is not a function, how do I call them properly?

fiddle

Upvotes: 2

Views: 115

Answers (2)

taguenizy
taguenizy

Reputation: 2265

If you use bracket notation you can achieve what you want

function getData(service, functionName){
    service[functionName](request, $root.thing).then()
}

Upvotes: 2

Ujjwal kaushik
Ujjwal kaushik

Reputation: 1686

function getData(service, functionName){
  var yourservice = $injector.get(service);
  yourservice.yourActualfunctionName(request, $root.thing).then() 
}    

You can sure use your service as string ,Use it like this.Make sure you inject $injector wherever you are using it. not sure about function name because function is not in same file.

Upvotes: 0

Related Questions