ngOnChanges doesn't work on two way data binding [(ngModel)]
See original GitHub issueComponent
import { Component, OnChanges } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnChanges {
title = 'app';
user = '';
ngOnChanges(): void {
console.log('user changed !');
}
}
View
<input type="text"
name="user"
id="user"
[(ngModel)]='user'>
<p>{{user}}</p>
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (2 by maintainers)
Top Results From Across the Web
ngOnChanges Angular doesn't work on two way data binding ...
IMO ngOnChanges only runs when the Input property changes come from a template binding for example <component [inputBinding]="someValue">.
Read more >On The Irrational Demonization Of Two-Way Data-Binding In ...
This component will accept an array of tags (strings); and, allow the user to enter new tags and remove existing tags. It will...
Read more >3 Ways to Pass Async Data to Angular 2+ Child Components
Solution 2: Use ngOnChanges. ngOnChanges is a lifecycle hook that run whenever it detects changes to input properties. That means it's ...
Read more >OnChanges - Angular
A lifecycle hook that is called when any data-bound property of a directive changes. Define an ngOnChanges() method to handle the changes.
Read more >ngModelChange and change events in Angular with examples
In Angular, We will use ngModel for two way data binding. Whenever a change happens in ngModel , Angular will trigger ngModelChange event....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@pixieaka
You probably missed some information. The
OnChanges
hook focusses on bounded inputs on a component level - the interface with outers. https://angular.io/api/core/OnChangesThe main root component of an application doesn’t have any bounded inputs, it’s a bootstrap element of the app, so the
OnChanges
on it has no sense. On any other component, it runs when any of its inputs have been changed (those inputs aren’t the form input tags).So probably you want to use the syntax
<input [ngModel]="user" (ngModelChange)="userChanged($event)" />
, where `userChanged
` is a method, that runs when a user changes the input value. It’s necessary to say, that there are 2 principal ways how to process forms. Template-based and reactive-based. https://angular.io/guide/forms-overview@mlc-mlapis I’ll take full course in angular on Udemy soon. All the best bro appreciated. I wish for you success for your future projects.