question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ngOnChanges doesn't work on two way data binding [(ngModel)]

See original GitHub issue

Component

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:closed
  • Created 3 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mlc-mlapiscommented, Jul 28, 2020

@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/OnChanges

The 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

1reaction
pixieakacommented, Jul 28, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found