Ask Your Question
1

What is the method of determining alterations in an @Input() value in Angular?

asked 2021-04-13 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2022-04-11 23:00:00 +0000

plato gravatar image

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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2021-04-13 11:00:00 +0000

Seen: 8 times

Last updated: Apr 11 '22