Angular2 Call Function When the Input Changes

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(){...}
}

Leave a Comment