我正在尝试将字符串转换为 Controller 中的 AngularJS 服务方法调用。例如,我想将字符串“Contact.send(email)”转换为调用现有的服务方法。我想使用:
window["Contact"]["send"](email);
如本帖 - How to execute a JavaScript function when I have its name as a string - 但它表示联系服务未定义,尽管已注入(inject) Controller 。
最佳答案
您需要使用 $injector 从字符串中获取服务:
$injector.get('Contact')['send'](email);
关于javascript - 在 AngularJS 中将字符串转换为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24817379/