preventDefault preventing ng-checked and ng-model bindings from working #15285
Comments
Angular needs the DOM events to do the bindings. When you prevent the click, Angular cannot set the checked. And why would it? You just prevented the click, the checkbox is not checked. This is expected behavior. When the user interacts with DOM elements, Angular tries to respect the regular rules of the DOM. |
I guess I'm a little confused as to what's happening here. It seems like a digest cycle still happens even after the |
You have two different variables in your code: isChecked, isClicked. Is that the cause of your confusion? |
Sorry, forgot to save my local changes to the plunker, should be using the correct variable name now, |
Looks the same too me. Here's an update: http://plnkr.co/edit/KOQo6ztxV1QTw5VfvjCA?p=preview |
Eh, not sure why the plunker isn't updating, but you can see the behavior I'm describing. |
The plnkr still doesn't do what I think you want - setting isChecked in the click handler instead of isClicked. Maybe you didn't save your plnkr or forked it before? But even if it does, the checkbox will not appear "checked", even if isChecked is set after preventDefault. I assume this is because the browser prevents the checking with preventDefault, and the subsequent change to the checked attribute comes too fast. Wrapping it in a timeout works: http://plnkr.co/edit/yVPKBCfXhgbhzurcqVxd?p=preview |
Due to lack of feedback, I'm going to close this as working as expected based on my explanation above |
I meet the same question. |
o |
Using preventDefault on an ng-click callback sends the state of a checkbox out of sync with ng-model and ng-checked bindings. Example here:
http://plnkr.co/edit/419QQthL8MmP2fQzVBog
Not quite sure why this happens, but the desired behavior is that the checkbox is checked when the bound value is true. There are a few use cases for this, for instance if you want to display modal warning before letting a user check a checkbox, or if you want another component to handle the model update that sets the checkbox state to checked.
The text was updated successfully, but these errors were encountered: