[英]How do ngfor to set a dynamic function in Angular 8?
i have this in my html component:我的 html 组件中有这个:
<div>
<button (click)="myFunction1()">myFunction1</button>
</div>
<div>
<button (click)="myFunction2()">myFunction2</button>
</div>
<div>
<button (click)="myFunction3()">myFunction3</button>
</div>
and in typescript:在 typescript 中:
myFunction1()
myFunction2()
myFunction3()
but i try to do something like this, but it's not working!但我尝试做这样的事情,但它不工作!
typescript component: typescript 组件:
var myFunctions = [
"myFunction1",
"myFunction2",
"myFunction3"
]
html component: html 组件:
<div *ngFor="let myFunction of myFunctions">
<button (click)="myFunction()">{{myFunction }}</button>
</div>
Know someone what the solution?知道有人有什么解决办法吗?
Do you want the function to execute on the button click?您希望 function 在按钮单击时执行吗? Just use the array to hold reference to the functions.只需使用数组来保存对函数的引用。
// *.ts
const myFunctions = [
myFunction1,
myFunction2,
myFunction3
]
Then in your template you can bind the click event to the function.然后在您的模板中,您可以将点击事件绑定到 function。
// *.html
<div *ngFor="let myFunction of myFunctions">
<button (click)="myFunction()">myFunction</button>
</div>
Your example above is just binding the click to a string.您上面的示例只是将单击绑定到字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.