我得到了一个函数名,想在 Angular 类(.ts 文件)中调用它。 eval()
不是一个选项。
我试过使用 window[functionName]
,但它失败了,猜测是因为窗口上下文在类之外
有什么想法吗?
最佳答案
这很容易。例如,getData
是您的函数名称。
getData() {
// your code goes here...
}
functionName = 'getData'; // function name as a string
现在你可以像下面这样调用这个函数:
this[functionName]();
关于javascript - 将字符串转换为 Angular 中的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59466212/