我在变量中有函数名,我在按钮的click事件上分配该变量。但它不起作用。任何帮助?
@Component({
selector: 'my-app',
template: `
<div>
<h2>Function name is: {{FunctionName}}</h2>
<input type="button" value="click here" (click) = [FunctionName]()>
</div>
<p>{{value}}</p>
`,
})
export class App {
FunctionName:string;
value: string;
constructor() {
this.FunctionName = 'clickFunction'
}
clickFunction(){
this.value = "button clicked";
}
}
这是代码
答案 0 :(得分:12)
语法必须如下:
<input type="button" value="click here" (click) ="this[FunctionName]()">
答案 1 :(得分:2)
试试这个
@Component({
selector: 'my-app',
template: `
<div>
<h2>Function name is: {{FunctionName}}</h2>
<input type="button" value="click here" (click)="FunctionName()">
</div>
<p>{{value}}</p>
`,
})
export class App {
FunctionName: Fn;
value: string;
constructor() {
this.FunctionName = this.clickFunction; //assign function to variable.
}
clickFunction(){
this.value = "button clicked";
}
}