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.

Checkbox [ngModel] does not reflect model value when updated in (onModelChange)

See original GitHub issue

I’m submitting a … (check one with “x”)

[x] bug report
[ ] feature request
[ ] support request

Current behavior <input type="checkbox" [ngModel]="foo" (ngModelChange)="onChanges()" /> Behaves exactly like: <input type="checkbox" (onChanges)="onChanges()" />

The checkbox only reflects the model value when it is first rendered, but then becomes functionally equivalent to a checkbox without a binding. Manually detecting changes or using setTimeout inside (ngModelChange) does not work around this issue. The same workarounds do work when used with (click), but voids the use of [ngModel].

Expected behavior The logic in onChanges should run, and the view should then be updated to reflect the model’s current value.

Minimal reproduction of the problem with instructions http://plnkr.co/edit/g9niPnLsUy6wWbzm0mHi?p=preview

What is the motivation / use case for changing the behavior? [ngModel] should always reflect the model value.

Please tell us about your environment: Mac OS X Sierra, IntelliJ IDEA

  • Angular version: 2.2.0

  • Browser: all

  • Language: [all | TypeScript X.X | ES6/7 | ES5] TypeScript 2.0.2

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:20
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
trotylcommented, Sep 6, 2018

@pcroc Issue confirmed, but it’s totally unrelated to checkbox, can be easily reproduced in input[type=text]:

https://stackblitz.com/edit/angular-pnpphi?file=src%2Fapp%2Fapp.component.ts

The problem is due to changing back to previous value, say:

  • Current value is "Angular";
  • After hitting backspace it become "Angula";
  • Handler manually changes it to "Angular";
  • Change detection find both previous and current input value being "Angular";
  • ngOnChanges not triggered due to no change;
  • The view value not being updated and become out of sync;

It’s more likely to happen in checkbox because there’re only two values available.

0reactions
pcroccommented, Sep 6, 2018

This is possibly a duplicate of #9275.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular : When ngModel data is updated from component, UI ...
i have created an logic to count if any of two checkbox is selected ... but the problem is that ng for doesn't...
Read more >
Angular Forms: Why is ngModelChange late when updating ...
In this snippet we're using the well known banana in a box syntax, which is the same as: [ngModel]='modelValue' (ngModelChange)='modelValue = $event' ....
Read more >
Difference between (change) and (ngModelChange) in Angular
First we need to understand that change is not an “Angular event”, ... the event callback fires and sets the new value to...
Read more >
Custom Checkbox Component with Angular - tsmean
The accessibility is bad, since it's not an input type="checkbox" element; ngModel and Angular forms cannot be used. This is where the Angular...
Read more >
ngModel Not Updating Input Value Resolved! - Freaky Jolly
Declaring a model value in the template, then passing it as a reference in method params, does not detect the updated model value;...
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