2

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

strwoc
  • 511
  • 1
  • 9
  • 18
  • Have you returned `functionName` from your service?? Is a `factory` or `service` ? – Davide Sep 27 '16 at 12:23
  • @Davide I pass the proper parameters, if that's what you're asking. Also console.log(service) and console.log(functionName) give me proper values. Yea it's a factory. – strwoc Sep 27 '16 at 12:24
  • What is `.then` ? Is a promise? Uhm are you sure that you return that function from your service? You are in controller when you call `getData` so? If you can provide a plunker we can help you better – Davide Sep 27 '16 at 12:26
  • I've edited the fiddle with the service and with the main controller part, I hope it helps. – strwoc Sep 27 '16 at 12:36
  • `myService` is a string. It is not a real service. It doesn't have `findData` method. Please, provide full code and not the excerpts, so it would be possible to note what exactly should be fixed in your code. – Estus Flask Sep 27 '16 at 12:39
  • Yes sure, you cannot use passing as string. Check this if it's work https://jsfiddle.net/jvx72dmd/1/ , but check this answer for better clarification http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string – Davide Sep 27 '16 at 12:43
  • @estus That is the whole code, I'm trying to pass the string in as a function name, the goal would be to have the string dynamically changed latter on by choosing the parameters from the url, and that is not a problem. – strwoc Sep 27 '16 at 12:43
  • Please, provide the entire controller in the question. This is the relevant part. – Estus Flask Sep 27 '16 at 12:46
  • https://jsfiddle.net/mfwvfv0L/1/ The whole controller, btw the window[param1][param2] didn't work, it just gave me the 'paramValue' is undefined – strwoc Sep 27 '16 at 12:48
  • Sure, so you can't pass `service` as string. You need a know service to reach your function simply with `service[functionName]` – Davide Sep 27 '16 at 12:51
  • It is not obvious that `getGridData` is supposed to use multiple services and why it is supposed to do this. More solid approach to inject desired services directly into controller. Any way, for truly random service use `var service = $injector.get(serviceName)`, as the answer suggests, and call its methods with `service[functionName]`. – Estus Flask Sep 27 '16 at 13:08

2 Answers2

2

If you use bracket notation you can achieve what you want

function getData(service, functionName){
    service[functionName](request, $root.thing).then()
}
taguenizy
  • 2,140
  • 1
  • 8
  • 26
0
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.

Ujjwal kaushik
  • 1,618
  • 11
  • 17