add
add

Reputation: 111

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);
  }  
});

Upvotes: 0

Views: 3574

Answers (3)

Priya
Priya

Reputation: 126

Try the following,

html:

<form class="list" ng-init="test={}" ng-submit="Pass(test)">

angular.js

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

codepen: http://codepen.io/mohanapriya/pen/BzpzZx?editors=1010

Upvotes: 0

add
add

Reputation: 111

I've managed to solve it, before reading the "accepted answer" post. See below for the solution I came up with also.

I've replaced the ng-value with value, and test.val with test.

http://codepen.io/angsar/pen/LpvGzj

angular.module('app', ['ionic'])
.controller('myCtrl', function($scope) {

  $scope.Pass = function($test) {
    alert("Test: "+$test);
  }; //pass  

});

Upvotes: 0

juuules_s04
juuules_s04

Reputation: 168

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>

Upvotes: 4

Related Questions