New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
state of the checkbox not getting updated even after its ng-model value is updated #9275
Comments
Please provide a plunker that allows to reproduce. |
Steps:
I have updated the ngModel in the ngModelChange method but its not getting reflected. |
There is a lot of code I'm sure could be removed and the problem still be reproduced. I think one of the channels mentioned in CONTRIBUTING - Got a Question or Problem? should be used instead. |
Updated Plunker with simplified logic The problem I am facing is - |
@IamKritika seems you didn't save the changes in the plunker, it's the same content as the first one. |
@zoechi can you check it now.. It seems to work fine for me. |
I can confirm that this is fishy. What's weird is that instead of |
@zoechi |
I think this is caused by change detection not seeing a change if the value is changed and immediately restored before change detection has a chance to run and detect the change. This is the reason the "new" value is not propagated back to the checkbox. The checkbox updates itself and Angular doesn't see a change that would make it necessary to propagate back to the checkbox. If you explicitly invoke change detection before the value is reset, then change detection kicks in (same with I think this is by design. |
Yes that's absolutely correct that angular doesn't see any change and hence the state of checkbox is unaltered . But then resetting the state of any ngModel would not be possible without explicitly invoking change detection (which I feel is a overhead) . |
I created a plunker illustrating the problem and an alternative solution using Controls for binding. It still would be nice if [ngModel]+(event) binding worked, because it means you can have only one event-handling method in the component for all the checkboxes in a list, while with Controls you have to create one per checkbox and subscribe to each of them. |
@Andikki plunker link is not opening. Can you share the correct url ? |
@IamKritika Sorry, markdown failure on my part. Fixed. |
Ran into something similar today. (change) is passed the state of the checkbox when it was clicked, not the new state. All the examples I see use $event to get around this issue. Click the checkbox a few times, expect the value [(ngModel)] is bound to and (change) to be in sync. They are not. |
@thejoecode Just use (ngModelChange) instead of (change) |
Facing the same problem today. @zoechi Do you think it's not a bug? |
@thejoecode looks like #3406 |
even I had this issue --> replaced (click) event with (ngModelChange) event. Now its working fine. |
I am also facing the issue related to checkbox list. i have to maintain the checkbox list checked on back from browser. But it is not working. please help me to resolve this issue. |
@vildantursic that does look like a different issue. This issue is quite old and not introduced with 6.0.3 |
@zoechi Thank you, I solved it somehow :) I have to share what caused problem Should I report this? |
@vildantursic remember that returning |
This took me forever to understand, but I finally fixed the issue. this.myArr = [...this.myArr]; Angular ChangeDetector: ChangeDetectorRef CPI's answer helped on stackoverflow helped. |
Still broken for Angular 7. |
Holy cow! It's 2020, Angular 9 is just around the corner and I am still experiencing this issue 😱 Decided to use a custom component that looks like a checkbox but uses a button underneath. |
i am still having the issue. Can anyone provide a solution or a workaround? Even when i inspect the Checkbox, the |
Guys, why this is still not working in Angular 8.2? It has to be fixed... |
Having this issue also EDIT: Fixed by using the (change) event instead of (click) |
Hi guys, i had a similar issue. I had some checkbox and one for check all others one. When i clicked over some checkbox it change its state properly but when i clicked over checkAll checkbox the above checkboxes did not change their state. I had to import jquery on my angular component (maybe is not the best solution but it works for me) and then my checkAll function was something like that, i had to fire the change event with trigger() function to change the checkbox state
My general checkbox is: And the others checkboxes
i used ViewChildren on my component for loop my checkboxes |
After spending 2 hours scratching my head, I have finally found a workaround: Change this: To this: toggleStatus function: Also solves #3406 Explanation:Though I'm not familiar how Angular handles input (checkbox in this case) state rendering, but this gives some idea: Angular would only change the state of a checkbox (ticked, unticked) when it detects a change in variable state ( Workaround: |
If y'all are having issues with the click event conflicting with your calculated |
Thanks, actually that really solved my issues. I was forced to use the vanilla (click) event for my changes as I was content projecting the checkbox while the parent element had another (click) event registered on itself. Using the Angular material API (changes) on the checkbox element would not trigger properly as this post already extensively discussed. I implemented the provider on the component itself via component decorator to localize. Applying this globally in the Material Module would break default functionality if you have checkboxes with (change) implemented. |
Current behavior
If you try and update the ngModel of a checkbox in say ngModelChange method, the ngModel value is updated but the checkbox still remains in previous state.
Expected/desired behavior
The state of checkbox should change as ngModel value changes.
I am trying to implement "select all" checkbox through angular 2.
I have a list of checkboxes and one select all checkbox. And in my use case it's so that if user checks all checkboxes from the list manually , I have to uncheck all the checkboxes from the list and check the "All" checkbox. So I have binded a ngModel and ngModelChange to all the checkboxes .Now what happens is if I try to update the ngModel in the ngModelChange method ,the state of checkbox is not changed even though the ngModel value is updated (since angular2 listens to events only and not to change in value of ngModel). I have tried to achieve this through the attribute directive as well but nothing seems to work.
The text was updated successfully, but these errors were encountered: