Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value of selected radio button in angularjs

Tags:

angularjs

I have two radio buttons pass and fail.

How to get value of selected radio button.

<div class="col-md-4">
            Result
            <div class="radio">
                <label><input type="radio" name="rdoResult">pass</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="rdoResult">fail</label>
            </div>
        </div>

Do I need to use ng-model or something else. In jquery I know the things well not in angularjs.

like image 808
Gaurav123 Avatar asked Sep 08 '15 06:09

Gaurav123


2 Answers

Here is your javascript

  <script>
    aap=angular.module('myApp',[])
    .controller('myCtrl',["$scope",function($scope){
      $scope.change='data';
      $scope.getVal=function(){

        console.log($scope.changedVal);
        $scope.change=$scope.changedVal;
      }
    }]);

  </script>

and your html

<body ng-app="myApp">
    <h1>{{1+1}}</h1>
    <div class="col-md-4" ng-controller="myCtrl">
            Result
            <div class="radio">
                <label><input type="radio" name="rdoResult" ng-model="changedVal" value="pass" ng-click="getVal()">pass</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="rdoResult" ng-model="changedVal" value="fail" ng-click="getVal()">fail</label>
            </div>
            {{change}}
        </div></body>

working demo

Hope this is what you are looking for.

like image 120
amoeba Avatar answered Oct 04 '22 20:10

amoeba


Both should have same ng-model with different ng-value(meant for use with select options or radio button), so that the selected value will be changed on result $scope variable and you can grab that value inside a controller on form submit or button click.

Markup

<div class="col-md-4">
    Result
    <div class="radio">
        <label>
            <input ng-model="result" type="radio" name="rdoResult" ng-value="'pass'">
              pass
        </label>
    </div>
    <div class="radio">
        <label>
            <input ng-model="result" type="radio" name="rdoResult" ng-value="'fail'">
              fail
        </label>
    </div>
</div>
like image 20
Pankaj Parkar Avatar answered Oct 04 '22 19:10

Pankaj Parkar