The ngOnChanges lifecycle hook is called when the values of bound properties (input properties) change. It receives a SimpleChanges object that provides information about the changes.
Here's an example demonstrating the use of ngOnChanges:
In this example:
- There is an initialMessage input property.
- The ngOnChanges method is implemented to handle changes to the input properties.
- Inside ngOnChanges, you can access the SimpleChanges object to check if the initialMessage property has changed.
- If the property has changed, you can access its previous and current values from the SimpleChanges object and update the component accordingly.
In a parent component's template, you might use this child component like this:
Whenever the value of parentMessage changes in the parent component, the ngOnChanges method in the child component will be triggered, allowing you to respond to the change in the input property.
.png)
.png)
No comments:
Post a Comment