【发布时间】:2018-02-14 10:23:00
【问题描述】:
我想在我的html 模板中从一组对象动态生成按钮,如下所示:
打字稿
private buttons: ButtonType[] = [];
ngOnInit(){
let button: ButtonType = new ButtonType();
button.name = "HOMEPAGE";
button.clazz = "btn-blue";
button.action = "goHome()";
this.buttons.push(button);
}
private goHome() {
this.router.navigate(["/"]);
}
html
<button *ngFor="let b of buttons" type="button" [class]="b.clazz" (click)="b.action">{{b.name}}</button>
除了action 部分之外,似乎运行良好,它什么都不做。 (click) 部分根本没有渲染。是否有一种绑定方式可以将b.action 参数解释为一种方法,就像我在html 页面内写(click)="goHome()" 一样?
【问题讨论】:
-
您可以尝试使用
button.action = goHome;(无引号,无括号)和(click)="b.action()"(括号)。它应该可以工作,但由于打字稿而不确定
标签: angular typescript data-binding