动态函数调用Angular 2 [英] Dynamic function calling Angular 2

查看:112
本文介绍了动态函数调用Angular 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个变量中有函数名称,并且在按钮的click事件上分配该变量.但它不起作用.有帮助吗?

I have function name in a variable and I am assigning that variable on click event of button. but it is not working. Any Help?

@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]()">

固定的插头: https://plnkr.co/edit/xGgFQuHNH72Q9FdOPEdK?p=preview

这篇关于动态函数调用Angular 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆