5

I am new in angularjs.I want to fetch the radio button value when user click a button,but my bad luck its not working.Here is the code

<label class="item item-radio">
    <input type="radio" name="group" ng-model="user.answer" value="{{questions.quiz_ans_opt1}}">
    <div class="item-content item-body">a) {{questions.quiz_ans_opt1}}</div><i class="radio-icon ion-checkmark"></i>
</label>
<label class="item item-radio item-body">
    <input type="radio" name="group" ng-model="user.answer" value="{{questions.quiz_ans_opt2}}">
    <div class="item-content item-body">b) {{questions.quiz_ans_opt2}}</div><i class="radio-icon ion-checkmark"></i>
</label>
<button class="button button-balanced button-block" ng-click="submitAnswer(user)">Submit</button>

Here is controller

$scope.submitAnswer = function(user) {
    //it output undefined error
    alert(user);
}

Also i want to disable the button until a radio button is checked,how can i achive this?

1
  • can you create a plunkr with your problem? Commented Nov 9, 2014 at 14:11

3 Answers 3

1

Please have a look at this,

Try this,

In html,

<body ng-controller="MainCtrl">
  <p>Hello {{name}}!</p>
  <input type="radio" ng-model="user.answer" ng-value="'option a'">
  <label>option a</label>
  <br>
  <input type="radio" ng-model="user.answer" ng-value="'option b'">
  <label>option b</label>
  <br>
  <input type="radio" ng-model="user.answer" ng-value="'option c'">
  <label>option c</label>
  <br>
  <input type="radio" ng-model="user.answer" ng-value="'option d'">
  <label>option d</label>
  <br>
  <button ng-disabled="!user.answer" ng-click="submitAnswer(user)">Submit</button>
</body>

In app.js,

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.submitAnswer=function(user){

    alert(user.answer);

  }
});

Here is the plunker demo for the same

Sign up to request clarification or add additional context in comments.

1 Comment

To prevent the multi-selection of the buttons ensure you have the same name for each radio buttons like: <input type="radio" ng-model="user.answer name="radio-btn" ng-value="'option a'">
0

Though a bit late, i would like to post a second approach, since you are using Ionic-Framework.

If you at all have a dynamic number of radio button options, that are being driven by backend data [Even if you have a static radio button set, you can convert it to the formal], Then you should instead use Ionic's Radio Buttons :

I.E, In your HTML :

<ion-radio ng-repeat="user in userAnswers"
           ng-value="user.answer"
           ng-model="finalAnswer">
  {{ item.text }}
</ion-radio>

And in your controller :

$scope.userAnswers = [
    { text: "Backbone", answer: "bb" },
    { text: "Angular", answer: "ng" },
    { text: "Ember", answer: "em" },
    { text: "Knockout", answer: "ko" }
  ];

  $scope.finalAnswer = 'ng';

More Details can be found in the official documentation and the CodePen Demo

Comments

0

This post really helped me out. I ended up changing all my radio button ng-click events to ng-change. Hope this helps.

AngularJS - Trigger when radio button is selected

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.