Angular2 Call Function When the Input Changes

前端 未结 1 1070
无人共我
无人共我 2020-12-28 15:13

Child component:

export class Child {
    @Input() public value: string;
    public childFunction(){...}
}

Parent component:



        
相关标签:
1条回答
  • 2020-12-28 15:57

    You can use the ngOnChanges() lifecycle hook

    export class Child {
        @Input() public value: string;
    
        ngOnChanges(changes) {
          this.childFunction()
        }
        public childFunction(){...}
    }
    

    or use a setter

    export class Child {
        @Input() 
        public set value(val: string) {
          this._value = val;
          this.childFunction();
        }
        public childFunction(){...}
    }
    
    0 讨论(0)
提交回复
热议问题