【问题标题】:How to execute a funtion on @Input parameter in angularjs2?如何在angularjs 2中对@Input参数执行函数?
【发布时间】:2017-03-11 22:21:58
【问题描述】:

我想在 @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 = '';
       }
    }

【问题讨论】:

    标签: javascript function angular input parameters


    【解决方案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 = '';
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多