Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Angular 4 the hook ngOnChanges is not firing on input

Tags:

angular

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)   }      } 
like image 678
simonwidjaja Avatar asked Jun 26 '17 21:06

simonwidjaja


People also ask

What triggers ngOnChanges?

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.

How does ngOnChanges work in Angular?

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.

How do you use ngOnChanges?

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.

Does ngOnChanges runs before ngOnInit?

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( ).


1 Answers

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.

like image 198
BeetleJuice Avatar answered Sep 21 '22 00:09

BeetleJuice