-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Wrong behaviour of radio input inside ng-repeat with double values #16690
Comments
This happens because you use the same name for all radio buttons in a group. This is what you need to do in traditional (non-AngularJS) apps, but AngularJS needs each radio to have a different name (they are grouped by their See #15009 for more info. |
Thx for your answer, I read the other issue. |
The behavior is expected. Whether the radios are checked or not (in an AngularJS form) depends on the current value (determined by |
I get the point. |
The reason it works (which may not be obvious at first - especially if you are not familiar with scope hierarchies and how prototypal inheritance comes into play), is that So, each What this actually means, is that they are independent as far as AngularJS is concerned (i.e. all of them could be selected at the same time and clicking the one does not affect the others through However, notice they don't actually work as expected (although they visually seem they do), since |
I knew that ng-repeat creates a new scope. I tought a scope for the iteration itself (1 for each ng-repeat declaration) would be created, not for the single iterated item. |
I'm submitting a ...
Current behavior:
I created a ng-repeat with a radio input inside. It bevavies strange when the ng-model of the radio is a nested object (ie. ng-model="data.fieldName"). Otherwise (ng-model="fieldName") it works fine.
Expected / new behavior:
As a radio usually behavies
Minimal reproduction of the problem with instructions:
Here the plunker.
The third radio works fine since it has no dupes. The first one has dupes but the ng-model is local and also works fine. The second one has dupes, is global and does not really works fine: Try to select 'Tetst2' and then select one of the 'Test' .
AngularJS version:
1.2.9 and 1.7.2
Browser:
Tested on Chrome, IE11, Edge. Same problem on all browsers
Anything else:
Here again the code:
<table ng-app="myApp" ng-controller="myCtrl">
<tr>
<td class="ms-cellstyle ms-vb2 hgv_data" ng-repeat="i in data">
<input type="radio" ng-model="thisWorks" name="Title" ng-value="i.Title">
<span>{{i.Title}}</span>
</td>
</tr>
<tr>
<td class="ms-cellstyle ms-vb2 hgv_data" ng-repeat="i in data">
<input type="radio" ng-model="data.thisDoesNotWork" name="Title" ng-value="i.Title">
<span>{{i.Title}}</span>
</td>
</tr>
<tr>
<td class="ms-cellstyle ms-vb2 hgv_data" ng-repeat="i in data">
<input type="radio" ng-model="data.selectedAnotherValue" name="AnotherValue" ng-value="i.AnotherValue">
<span>{{i.AnotherValue}}</span>
</td>
</tr>
</table>
`var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', ['$scope', function ($scope) {
$scope.data = [{
ID: 1,
Title: 'Test',
AnotherValue: 'hello'
}, {
ID: 2,
Title: 'Test2',
AnotherValue: 'its'
}, {
ID: 3,
Title: 'Test',
AnotherValue: 'me'
}];
}])`
The text was updated successfully, but these errors were encountered: