Amir Talic Amir Talic - 2 months ago 9
Javascript Question

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

Answer

If you use bracket notation you can achieve what you want

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