Detecting Input Binding Changes
Explore how Angular's OnChanges lifecycle hook detects changes in input bindings within components. Understand implementing the ngOnChanges method to track old and new values, and use the AfterViewInit hook to access child components. This lesson helps you manage component communication and lifecycle events for better user experiences.
We'll cover the following...
The OnChanges lifecycle hook is called when Angular detects that the value of an input data binding has changed. We will use it in the product detail component to learn how it behaves when we select a different product from the list:
Import the
OnChangesandSimpleChangesartifacts in theproduct-detail.component.tsfile.
Modify the definition of the
ProductDetailComponentclass so that it implements theOnChangesinterface.
Implement the
ngOnChangesmethod that is defined in theOnChangesinterface. It accepts an object of typeSimpleChangesas a parameter that contains one key for each input property that changes. Each key points to another object with the propertiescurrentValueandpreviousValue, which denote the new and the old value of the input property, respectively.
The previous snippet tracks the name ...