Ege Bayrak Ege Bayrak - 2 months ago 5
AngularJS Question

getting value of inputs created by ng-repeat

I created a radiobutton input but struggling to get the values of selected inputs.

this is the radiobuttons input I created

<ul ng-repeat="item in riskModel">
<li>
<input type="radio" name="rdbRisk" ng-value="{{item.value}}" />
{{item.text}}
</li>
</ul>


and this the ng-model definiton in controller

$scope.riskModel = [
{ id: 0, text: "A", value: "" },
{ id: 1, text: "B Risk", value: "YR" },
{ id: 2, text: "C Risk", value: "OR" },
{id:3,text:"D Risk",value:"DR"}
]


I want to take the selected value and send it to a function as a parameter but so far I failed to get any results.

Answer

Declare your ng-model variable as object $scope.radioModel = {};

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

myApp.controller('GreetingController', ['$scope', function($scope) {
  //$scope.radioModel = {};
  $scope.riskModel = [
        { id: 0, text: "A", value: "" },
        { id: 1, text: "B Risk", value: "YR" },
        { id: 2, text: "C Risk", value: "OR" },
        {id:3,text:"D Risk",value:"DR"}
    ]
  $scope.changeValue = function(value){
    console.log(value.selected);
  }
  
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
  <div ng-controller="GreetingController" ng-init="radioModel={}">
    Get selected value : {{radioModel.selected}}
  <ul ng-repeat="item in riskModel">
    <li>
      <input type="radio" ng-model="radioModel.selected" name="rdbRisk" ng-value="item.value" ng-change="changeValue(radioModel)"/>
      {{item.text}}
    </li>
</ul>
</div>
</body>

Comments