NewLuckyProgramer
NewLuckyProgramer

Reputation: 23

get value from ng-repeat radio buttons

I have ng-repeat of radio buttons:

<div ng-repeat="c in currencies">

        <ion-radio   ng-model="checkradio" ng-value='c.code'  >
           {{c.name}} {{c.code}}
    </ion-radio>
          </div>

I intend to $watch the model and the value of the selected radio buttons:

$scope.$watch('checkradio', function  () {


      $scope.checkradio = "";

})

I don't get any value from my radio buttons. Obviously, I'm doing something wrong. I tried other approaches but they didn't work. Can anyone suggest how to get the value from the radio buttons?

Upvotes: 0

Views: 4412

Answers (2)

gianlucatursi
gianlucatursi

Reputation: 680

For binding you should have an object not a variabile. Then in your controller you should have:

$scope.radios = [{name:"first", code:"1"}, {name:"second", code:"2"}];
$scope.model = { checked: "1" };//check the first element

and in your HTML:

<ion-radio ng-repeat="radio in radios" ng-model="model.checked" ng-value="radio.code">{{radio.name}}</ion-radio>

or

<div ng-repeat="c in currencies">
    <ion-radio   ng-model="model.checked" ng-value='c.code'  >
       {{c.name}} {{c.code}}
    </ion-radio> 
</div>

You can find a codepen here

Upvotes: 2

Devidas Kadam
Devidas Kadam

Reputation: 944

try this, this may help you, Here is working fiddle

  <div ng-controller="MyCtrl">
<div ng-repeat="(key,val) in currencies">
      <input type="radio"  ng-model="$parent.checkradioasd" ng-value='val.code'  >
         {{val.name}}
 </div> <br>
 <div>Selected : {{checkradioasd}}</div>

controller

  var myApp = angular.module('myApp',[]);

 function MyCtrl($scope) {
  $scope.currencies = [{name:'Option 1',code:1},{name:'Option 2',code:2}];
  $scope.checkradio = $scope.currencies[0].code;
}

Upvotes: 1

Related Questions