I am pretty new to Angular2/4. I have created a basic component with an input field. The user input will be reflected in the other components that are bound to the user input. Now I want to setup a general listener to a value change.
I thought the OnChanges hook would be the perfect solution but ngOnChanges method was NEVER called. Am I doing something wrong?
Any hint appreciated...
import { Component, Input, SimpleChanges, OnChanges } from '@angular/core'; @Component({ selector: 'my-app', template: ` <input [(ngModel)]="name"> <input [(ngModel)]="name"> <h1>{{name}}</h1> `, }) export class AppComponent implements OnChanges { @Input() name: String = "abc" ngOnChanges(changes: SimpleChanges) { // changes.prop contains the old and the new value... console.log('on change', changes) } }
ngOnChanges triggers following the modification of @Input bound class members. Data bound by the @Input() decorator come from an external source. When the external source alters that data in a detectable manner, it passes through the @Input property again.
The ngOnChnages is a life cycle hook, which angular fires when it detects changes to data-bound input property. This method receives a SimpeChanges object, which contains the current and previous property values. The child Component decorates the property using the @Input decorator.
When should you use ngOnChanges? Use ngOnChanges whenever you want to detect changes from a variable decorated by @Input. Remember that only changes from the parent component will trigger this function. Also remember that changes from the parent still update the child value even without implementing ngOnChanges.
ngOnChanges( ) — It is called before ngOnInit( ) and whenever one or more data-bound input properties change. It detects simple changes in the property values. ngOnInit( ) — It initializes the directive/component after Angular displays the data-bound properties and is called once after ngOnChanges( ).
ngOnChanges
is not called every time a component property changes internally. It gets called when the databinding from the parent component pushes a new value into the child component. So if your parent component has
<child-comp [name]="parentValue"></child-comp>
When parentValue
changes, the child component's @Input() name
will change and that will trigger ngOnChanges
Take a look at the docs:
ngOnChanges
Respond when Angular (re)sets data-bound input properties...Called before ngOnInit() and whenever one or more data-bound input properties change.
To be notified when your form inputs change, in the long run the best approach is to learn Reactive Forms because they make you create the FormControl
objects in your component and expose a very simple observable that emits every time the form value changes.
If you wish to stick with template-driven forms, you can bind to the input's keypress
or keyup
event to fire whatever logic you want to run on change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With