Sitemap
Pravin M

Pravin M

I am a frontend developer with 10+ years of experience Blog :- https://www.frontendinterviewquestions.com

Member-only story

OnChanges in Angular

3 min readJul 17, 2024

--

Press enter or click to view image in full size
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

  • ngOnChanges is called before the ngOnInit lifecycle hook.
  • It receives a SimpleChanges object 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.

Step 1: Create a New Angular…

--

--

Pravin M
Pravin M

Written by Pravin M

I am a frontend developer with 10+ years of experience Blog :- https://www.frontendinterviewquestions.com