How to set radio button in angularjs with Object -
i have created radio buttons in angularjs using array of objects using ng-value , ng-model.
`
<div ng-controller="democontroller"> <div ng-repeat="detail in details"> <input type="radio" ng-model="$parent.selectedval" ng-value="detail" name="test"> </div> {{selectedval}} </div>
` on selection of radio button model populated corresponding object value. not sure how initialize using controller object.
var app = angular.module('myapp', []); app.controller("democontroller",democontroller); function democontroller($scope) { $scope.selectedval={name:"def",age:4} ; $scope.details=[{name:'abc',age:2},{name:'xyz',age:3},{name:'def',age:4}]; }
you should reference selected value details array instead of creating new instance:
$scope.details = [{ name:'abc', age:2 }, { name:'xyz', age:3 }, { name:'def', age:4 }]; $scope.selectedval = $scope.details[2];
this select last of 3 radio buttons. if both details
, selectedval
defined within same scope should update radio button well, it's quite unclear why referenced selectedval
$parentscope
:
<input type="radio" ng-model="selectedval" ng-value="detail" name="test" />
and here's updated fiddle: https://jsfiddle.net/sb3krc8c/2/
Comments
Post a Comment