Skip to content Skip to sidebar Skip to footer

Angular2 Call Function When The Input Changes

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

Solution 1:

You can use the ngOnChanges()lifecycle hook

exportclassChild {
    @Input() publicvalue: string;

    ngOnChanges(changes) {
      this.childFunction()
    }
    publicchildFunction(){...}
}

or use a setter

export classChild{
    @Input()publicset value(val: string) {
      this._value = val;
      this.childFunction();
    }
    public childFunction(){...}
}

Post a Comment for "Angular2 Call Function When The Input Changes"