Angular Radio Values ​​in ng-repeat

I am new to Angular and trying to capture the selected radio value, but when using ng-repeat the documentation is fuzzy. Any help would be greatly appreciated.

<div ng-repeat="item in ed"> <label for="{{item['code']}}"> <input ng-change="getPlanTypes()" ng-model="ed" type="radio" id="{{item['code']}}" name="effective_date" value="{{item['code']}}"> {{item['date']}} </label> </div> 

Here is the controller, but I'm not sure about the correct choice of the selected radio value?

 rates.controller('getEffectiveDates', function($scope, $http, $location, myService, localStorageService) { myService.effective_dates().then(function(ed) { $scope.ed = ed; }); $scope.getPlanTypes = function() { console.log($scope.ed['code']); //Futile attempt that returns undefined localStorageService.add('code',$scope.ed['code']); $location.path("/plan-types"); } }); 
+4
source share
2 answers

Do

 ng-click="getPlanTypes(item.code)" 

and in your controller you can get the value

 $scope.getPlanTypes = function (ed) { console.log(ed); } 
+3
source

http://jsfiddle.net/2LZpv/

HTML

 <div ng-app="myApp" ng-controller="getEffectiveDates"> <div ng-repeat="item in ed"> <label for="{{item['code']}}"> <input ng-click="getPlanTypes(item)" ng-model="ed" type="radio" id="{{item['code']}}" name="effective_date" value="{{item['code']}}"/> {{item['date']}} </label> </div> </div> 

Js

 angular.module("myApp",[]).controller('getEffectiveDates', ["$scope", function($scope) { $scope.ed = [{code:'1',date:"test date 1"},{code:'2',date:"test date 2"}]; $scope.getPlanTypes = function(selectedItem) { console.log(selectedItem["code"]); //Feeble attempt that returns undefined } }]); 
0
source

Source: https://habr.com/ru/post/1495005/


All Articles