动态函数调用 Angular 2
Dynamic function calling Angular 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:string;
value: string;
constructor() {
this.FunctionName = 'clickFunction'
}
clickFunction(){
this.value = "button clicked";
}
}
这是代码
语法需要像这样:
<input type="button" value="click here" (click) ="this[FunctionName]()">
试试这个
@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";
}
}
我在一个变量中有函数名称,我在按钮的单击事件上分配该变量。但它不工作。有帮助吗?
@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";
}
}
这是代码
语法需要像这样:
<input type="button" value="click here" (click) ="this[FunctionName]()">
试试这个
@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";
}
}