Sara
Sara

Reputation: 2436

How to know which of the radio buttons is selected in angularjs

I have 2 radio buttons like this, and 2 text boxes that should be enabled based on the choice on the radio buttons.

 <input type="radio" name="type" ng-model="outputType" value="1"> Choice1 <br/>
 <input type="radio" name="type" ng-model="outputType" value="2"> Choice2 <br/>

<td><input type="text" ng-model="name" value="name" ng-disabled="istypeSelected('1') " size="5"> Name <br/></td>

<td><input type="text" ng-model="date" value="date" ng-disabled="istypeSelected('2')" size="5"> date <br/></td>

and here is a button

<button class="btn" type="button" style="" ng-click="update()">update</button>

I want to run some sql query in the back end when user hit the button. But I need to know which of the radio buttons has been chosen also. How can I access it?

Upvotes: 0

Views: 607

Answers (1)

mike
mike

Reputation: 36

In your controller, you should simply be able to access $scope.outputType and that will take the value of the radio button (i.e. 1 or 2) and assign it to whatever variable you wish to assign it to.

i.e. var myChoice = $scope.outputType;

http://docs.angularjs.org/api/ng/input/input%5Bradio%5D

EDIT Hard to say what's up, because I couldn't see your controller. The following displays just fine in the console for me:

var myapp = angular.module('myapp',[]);
myapp.controller('FormCtrl', ['$scope',
   function($scope) {
    $scope.update = function() {
        var myChoice = $scope.outputType;
        console.log(myChoice);
    };
}]);

And, you also need to assign which controller to your form element:

form ng-controller='FormCtrl'

Upvotes: 2

Related Questions