How to detect when an @Input() value changes in Angular?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Angular, component interactions often rely on the use of @Input() properties to pass data from a parent component to a child component. Detecting changes in these input properties is essential for performing operations like updating the child component's display or processing the data received. Angular provides several methods to handle changes in input properties effectively.
Using ngOnChanges Lifecycle Hook
One of the primary ways to detect changes in input properties in Angular is by using the ngOnChanges lifecycle hook. This hook is called whenever one or more data-bound input properties change. The method receives a SimpleChanges object that contains the current and previous property values.
Here is a basic example:
In this example, the SampleComponent listens for changes to the name input property. Inside the ngOnChanges method, a check is performed to see if the name input property has changed, and if so, it logs the change.
Using a Setter for @Input()
Another way to react to changes in @Input() properties is to use a setter. This approach offers more control over a single or specific property and is useful when you need to execute some logic as soon as the property changes.
Here’s how you can implement it:
In this example, any change to the name input property immediately triggers the setter, allowing you to run custom logic (here, simply logging the new name).
Using ngDoCheck Lifecycle Hook
For more control or more complex scenarios, you can use the ngDoCheck lifecycle hook. This method is called with every change detection cycle, so it provides a place to implement custom change detection logic.
While powerful, ngDoCheck can lead to performance issues if not used carefully due to its frequent invocation.
Summary Table
| Method | Usage Scenario | Efficiency |
ngOnChanges | Detecting changes to one or more data-bound input properties | High |
Setter for @Input | Specific logic on property change, control over a single property | Medium to High |
ngDoCheck | Complex scenarios, custom tracking needs | Low (Cautious use advised) |
Conclusion
Detecting changes to @Input() properties in Angular can be accomplished through various methods depending on the complexity and specific requirements of your application. Using lifecycle hooks like ngOnChanges and ngDoCheck, or employing property setters, provide robust options to handle incoming data efficiently and reactively in your Angular components.
Related reading
- How to determine if a JavaScript array contains an object with an attribute that equals a given value
- How to determine if object exists AWS S3 Node.JS sdk
- How to disable a link using only CSS
- how to disable rotation in React Native?
- How to dispatch a Redux action with a timeout?
- How to display an image
- How to display Base64 images in HTML
- How to display HTML in TextView?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.