In Angular, you can use ngOnChanges lifecycle hook to determine alterations in an @Input() value. This hook is called whenever there is a change detected in any of the component’s input properties.
Here’s an example of how to use ngOnChanges hook:
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
@Component({
selector: 'app-my-component',
template: `
<div>{{myInput}}</div>
`
})
export class MyComponent implements OnChanges {
@Input() myInput: string;
ngOnChanges(changes: SimpleChanges) {
if (changes.myInput) {
console.log('myInput has changed:', changes.myInput.currentValue);
}
}
}
In the above example, ngOnChanges hook captures the changes made to the myInput property and logs it to the console.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-04-13 11:00:00 +0000
Seen: 8 times
Last updated: Apr 11 '22