javascript - 如何在 angularjs 2 中的 @Input 参数上执行函数?

标签 javascript function angular input parameters

我想在 @Input() cleanTextBox 上执行 doClean() 函数

    @Component({
      selector: 'my-component',
      providers: [],
      template: `<input [(ngModel)]="abc">`,
      directives: []
    })
    export class Directive {
      @Input() cleanTextBox : boolean;
      public abc = "someValue";

     // Execute doClean function if cleanTextBox is true
       public doClean{
           this.abc = '';
       }
    }

最佳答案

您可以使用 setter

export class Directive {

  private _cleanTextBox: boolean;

  @Input() set cleanTextBox(value: boolean) {
    if (value) {
      this.doClean();
    }
    this._cleanTextBox = value;
  }

  get cleanTextBox() {
    return this._cleanTextBox;
  }

  public abc = "someValue";

  public doClean{
    this.abc = '';
  }
}

关于javascript - 如何在 angularjs 2 中的 @Input 参数上执行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40315002/

相关文章:

javascript - 如何在浏览器 Javascript 中创建另一个时区的日期

javascript - HTML 文件输入接受特定尺寸的图像

javascript - 如何延迟函数的一部分?

css - 如何使用 ng2-bootstrap 覆盖 angular2 中的 twitter bootstrap 样式

Angular 变化检测: how to refresh only certain items?

javascript - 正则表达式 Javascript 数字逗号分隔 | > | >= <=

javascript - 虚拟 dom 何时比手动操作更快?

python - 从 scipy 根查找/优化函数返回多个值

mysql - PL-SQL - 我可以将枚举作为函数参数传递吗?

angular - 为什么我们将 [formGroup] 指令应用于方括号下的表单,而对于表单字段,我们应用不带方括号的 formControlName 指令?