-
Notifications
You must be signed in to change notification settings - Fork 27.4k
$viewChangeListeners not called with <input type="radio"> #15696
Comments
I see angular 1.0.1 on your jsFiddle. I've updated to 1.6.1 and appears to work http://jsfiddle.net/ADukg/10030/ |
@edenferreira I updated the fiddle. The bug is still present. In older versions of angular, step 4 worked and step 3 didn't, in newer versions, it's the opposite, but the bug is always present. |
@lovasoa, the problem is that controls are registered on their parent form by their name. Having the same name on one than one controls, the last one registered (usually the last to appear in DOM) overwrites all the others. This is what is happening here. Note that (unlike "traditional" HTML forms), readio buttons do not need the same name on AngularJS forms. You can either give them unique names or omit the name property altogether and let AngularJS assign unique names automatically. Since each control has its own instance of Closing since everything works as expected, but feel free to continue the discussion below. |
This is still at least very confusing. The documentation states that $viewValueListeners is an The documentation also states that
Here, the result is clearly different from what we would get by placing a $watch on the model value. |
Actually, you are right: Something is wrong. Even if the |
I think the problem is with the way inputs are added to their parent form controller. They are added by name, which means since the radio inputs have the same name, what you have in formCtrl.bool is the second radios ngModelController . However, each radio still has its own ngModelController, so in your example you are only adding the viewChangeListener to the second radio. Specifically we should have named radio groups that behave like a group, but I wonder if we can do that without a breaking change. |
That's what I thoguht it was at first, but this is different actually. Even if you change the names, the problem is that the Basically, the I haven't looked into it yet, but the current behavior is indeed weird/inconsistent 😞 |
Oh, that's interesting. I assume then we need to switch from listening on click to listening on change - would be breaking, though: angular.js/src/ng/directive/input.js Line 1819 in 71f437c
Same issue might exist for check boxes? Or we could listen on blur, too? |
Look what I found: #14685 There's a problem with ios8 here ... |
I think the As expected, the The radio buttons are kind of special in that you can't change their state (unselect) and thus update the bound model directly via interaction with the control itself. Imagine the following radios: <input type="radio" name="a" value="foo" ng-model="modelValue" />
<input type="radio" name="b" value="bar" ng-model="modelValue" /> You can set In that regard (and since Note, that the above radio button scenario is equivalent to having two textfields bound to the same model value (so typing into one will update the other). In this case too, It might be confusing that Quoting the docs:
Having said all that, I would like to change my official view to "works as expected" 💼 |
I am going to repeat myself, but the docs say that
and
And you are saying that
Reading the docs, the behavior you describe is not the one that is expected. |
And working around this by using different names for the same radio buttons group is far from being an ideal work-around, as angular won't change the |
I am afraid that is what exactly I am saying 😃 And (afaict) I'll let @Narretz confirm that I have not missed something and then we can update the docs 😁 |
But in the case of the input buttons, the change is indeed coming from the view. In my opinion, the behavior described here is clearly a bug. |
In the case of radio buttons, I am not making any programmatic change. |
As explained in previous comment, in Angular when you click radio B it updates the model value (change from view -> model) and fires B's If you need to be notified for changes in the bound model, you can either add |
Based on the discussion above (and since the docs have been updated to better reflect what |
Bug report
How to reproduce
What is the current behavior?
An alert appears ONLY after step 3.
What is the expected behavior?
An alert should appear after step 3 and 4.
The functions specified in
$viewChangeListeners
should be called every time the view value changes, as stated in the documentationWhich versions of AngularJS, and which browser / OS are affected by this issue?
The fiddle demonstrates the bug with the latest snapshot of angular.
Tested in both firefox and chrome.
Other information (e.g. stacktraces, related issues, suggestions how to fix)
The issue was reported here on stackoverflow: http://stackoverflow.com/questions/34703156/only-last-radio-button-triggers-viewchangelistener
The text was updated successfully, but these errors were encountered: