简体   繁体   English

如何在 angular HTML Template 中 (click) = "{{ DynamicFunctionCall }}" 动态传值?

[英]How to pass value dynamically in (click) = "{{ DynamicFunctionCall }}" in angular HTML Template?

In an angular HTML template, I am trying to pass the function name dynamically as the value of在 angular HTML 模板中,我试图动态传递 function 名称作为值

(click) = "{{dynamicFunc Name}}" 

but it is giving interpolation error.但它给出了插值错误。

Is there any way if we can make it work?有什么办法可以让它发挥作用吗?

No, this syntax is incorrect.不,这个语法不正确。 In Angular templates, you can bind to component properties or methods using the following syntax:在 Angular 模板中,您可以使用以下语法绑定到组件属性或方法:

<button (click)="dynamicFuncName()">Click me</button>

You cannot use interpolation syntax {{}} inside the event binding syntax ().您不能在事件绑定语法 () 中使用插值语法 {{}}。

You can use a function inside the interpolation without (click) directive like below.您可以在插值内部使用 function,无需像下面这样的 (click) 指令。 But I don't advise it.但我不建议这样做。 İnstead of it, check about Angular Pipe.而不是它,请检查 Angular Pipe。

<div>{{dynamicFuncName()}}</div>

If you wish dynamic functions name on element.如果您希望在元素上使用动态函数名称。 Follow this code:请遵循以下代码:

<button (click)="this[dynamicFuncNamesArray[0]]()">Click me</button>


 dynamicFuncNamesArray = ['dynamicFuncName'];
  dynamicFuncName() {
    console.log('dynamicFunc');
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM