Member-only story
OnChanges in Angular
For more questions and answers visit our website at Frontend Interview Questions
In Angular, the ngOnChanges lifecycle hook is one of the essential hooks used for detecting and acting upon changes to input properties of a component. This article will explain what ngOnChanges is, how it works, and how to use it effectively with practical examples.
What is ngOnChanges?
ngOnChanges is a lifecycle hook that is called when any data-bound input property of a directive changes. It allows you to respond to changes in the input properties and perform necessary actions based on these changes. This hook is particularly useful when you need to perform operations or update the component's state whenever an input property changes.
How ngOnChanges Works
ngOnChangesis called before thengOnInitlifecycle hook.- It receives a
SimpleChangesobject that contains the current and previous values of the input properties. - This hook is invoked whenever Angular sets or resets data-bound input properties.
Example: Using ngOnChanges in Angular
Let’s illustrate how ngOnChanges works with a practical example.









