[英]Angular interpolation function name with (click) Event
I got a JSON file where I specify dynamic different button but clicking on button does not call the function.我得到了一个 JSON 文件,我在其中指定了动态不同的按钮,但单击按钮不会调用该函数。
Here is my JSON file structure:这是我的 JSON 文件结构:
export const liveButtonData = [
{ title: 'My Name', function: 'getName()'}
{ title: 'My Title', function: 'getTitle()'}
{ title: 'My Status', function: ''}
}];
In my Angular HTML code在我的 Angular HTML 代码中
// the following clicking on the button does nothing // 下面点击按钮什么都不做
<button title={{button.title}}
(click)=button.function>
</button>
or要么
<button title={{button.title}}
(click)="button.function">
</button>
// this is not allowed // 这是不允许的
// the following throws an error
<button title={{button.title}}
(click)={{button.function}}>
</button>
Not sure what other syntax to try.不确定要尝试什么其他语法。 Any help is appreciated and thanks in advance.
感谢您提供任何帮助,并提前致谢。
In your case the JSON file holds the function names as:在您的情况下,JSON 文件将函数名称保存为:
Possible solution:可能的解决方案:
export const liveButtonData = [
{ title: 'My Name', function: 'getName'}
{ title: 'My Title', function: 'getTitle'}
{ title: 'My Status', function: ''}
}];
<button title={{button.title}}
(click)="functionWrapper(button, button.function)">
</button>
function functionWrapper (functionSource, functionName) {
functionSource[functionName]();
}
Then when we have the name/key of the function.然后当我们有了函数的名称/键时。 We take it from it's source object with [].
我们使用 [] 从它的源对象中获取它。 And call it with () at the end.
最后用 () 调用它。 And in the case above the () are not a string but real JavaScript invoke.
在上面的例子中, () 不是字符串而是真正的 JavaScript 调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.