Child component:
export class Child { @Input() public value: string; public childFunction(){...} }
Parent component:
export class Parent { public value2: string; function1(){ value2 = "a" } function2(){ value2 = "b" } }
Parent view:
<child [value]="value2">
Is there any way to call childFunction() every time the value2 is changed in this structure?
The "input" event, unlike the "change" event, is a synchronous event that is triggered as the user is interacting with the text-based input controls.
Calling a function or initializing a single value on page load in AngularJS is quite easy. AngularJS provides us with a dedicated directive for this specific task. It's the ng-init directive. Example 1: In this example, we will call a function to initialize a variable on page load.
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(){...} }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With