This particular snippet of code generates two sets of radio buttons. The first set consists of individual radio buttons, while the second set is dynamically created using ng-repeat. Users can select any of the radio buttons by clicking on them directly or by pressing the corresponding A-C buttons.
An issue arises when the ng-repeat radio buttons are clicked: they no longer respond to the A-C buttons for selection. Why does this happen?
angular.module('radioExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.color = {
name: 'blue'
};
$scope.radiomodel = 'blue';
$scope.radiovalues = ['red', 'green', 'blue'];
$scope.clickA = function() {
$scope.color.name = 'red';
$scope.radiomodel = 'red';
}
$scope.clickB = function() {
$scope.color.name = 'green';
$scope.radiomodel = 'green';
}
$scope.clickC = function() {
$scope.color.name = 'blue';
$scope.radiomodel = 'blue';
}
$scope.radioclick = function(index) {
$scope.clickvalue = index;
}
$scope.radiochange = function(index) {
$scope.changevalue = index;
}
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js"></script>
<body ng-app="radioExample">
<form name="myForm" ng-controller="ExampleController">
<button ng-click="clickA()">A</button>
<button ng-click="clickB()">B</button>
<button ng-click="clickC()">C</button>
<br/> Individual <br/>
<label>
<input type="radio" ng-model="color.name" value="red">
Red
</label><br/>
<label>
<input type="radio" ng-model="color.name" value = "green">
Green
</label><br/>
<label>
<input type="radio" ng-model="color.name" value="blue">
Blue
</label><br/> ng-repeat
<div ng-repeat="i in [0, 1, 2]">
<input type="radio" name="myradio" value="{{radiovalues[$index]}}" ng-model="radiomodel" ng-click="radioclick($index)" ng-change="radiochange($index)"> {{$index}} ({{radiovalues[$index]}}) </input>
</div>
<tt>color = {{color.name | json}}</tt><br/>
</form>
</body>