add add - 5 months ago 49
AngularJS Question

Ionic AngularJS Radio Group ng-model issue using ion-radio

I'm unable to retrieve the value of the Radio Group using AngularJS 1.4.7 with Ionic 1.1.1;

Any ideas would be great, thanks.

See codepen snippet of the issue: http://codepen.io/angsar/pen/RWdvrO

HTML

<html ng-app="app">
<head>
<link rel="stylesheet" href="http://code.ionicframework.com/1.1.1/css/ionic.min.css" />
<link href="http://code.ionicframework.com/ionicons/1.5.2/css/ionicons.min.css" rel="stylesheet">
<script src="http://code.ionicframework.com/1.1.1/js/ionic.bundle.js"></script>
</head>
<body ng-controller="myCtrl">
<ion-content padding="true">

<form class="list" ng-submit="Pass(test)">
<p>Selected Sample: {{test.val}}</p>
<ion-radio ng-model="test.val" ng-value="s1">Sample 1</ion-radio>
<ion-radio ng-model="test.val" ng-value="s2">Sample 2</ion-radio>
<button class="button button-energized button-large icon-right button-full">Test</button>
</form>

</ion-content>
</body>
</html>


Javascript

angular.module('app', ['ionic'])
.controller('myCtrl', function($scope) {
$scope.Pass = function($test) {
alert("Test: "+$test.val);
}
});

Answer

Try the following:

angular.module('app', ['ionic'])
.controller('myCtrl', function($scope) {
 $scope.test = {};
 $scope.Pass = function($test) {
   alert("Test: "+ $scole.test.val);
 }  
});

Explanation:

  1. $test is not defined in your $scope.
  2. variables starting with $ have a special meaning in angular, so you shouldn't use them as regular variable names

Update: It's also important to singelquote a string value within the double quoted ng-value

<ion-radio ng-model="test.val" ng-value="'s1'">Sample 1</ion-radio>
Comments