如何在angularjs2中对@Input参数执行功能?

时间:2016-10-29 00:56:27

标签: 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 = '';
       }
    }

1 个答案:

答案 0 :(得分:1)

你可以使用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 = '';
  }
}