Dynamic function calling Angular 2

拥有回忆 提交于 2019-12-01 08:46:36

Syntax needs to be like this:

<input type="button" value="click here" (click) ="this[FunctionName]()">

Fixed plunker: https://plnkr.co/edit/xGgFQuHNH72Q9FdOPEdK?p=preview

try this

@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";
  }
}

Online demo: https://plnkr.co/edit/6uVZd0L0KlwMdaIKgPXq?p=preview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!