admin管理员组

文章数量:1323003

I am using radio buttons and I want to know if they are checked or not in the controller. Is there any other way to find that out in angular except ( document.getElementById( "radio_button" ).checked ) ?

I am using radio buttons and I want to know if they are checked or not in the controller. Is there any other way to find that out in angular except ( document.getElementById( "radio_button" ).checked ) ?

Share Improve this question edited Jun 26, 2015 at 3:59 asked Jun 26, 2015 at 2:07 user2851669user2851669 3
  • Please post your checkbox html code. – Ajay Narain Mathur Commented Jun 26, 2015 at 2:09
  • 3 Use ng-model. See docs.angularjs/api/ng/input/input%5Bradio%5D – Phil Commented Jun 26, 2015 at 2:15
  • Note: checkbox and radio are different. Please make sure which one you want to use. – Icycool Commented Jun 26, 2015 at 3:07
Add a ment  | 

3 Answers 3

Reset to default 4

You can trigger the function on click like this :

In HTML :

  <div  ng-click='newValue(value)'>
                <input type="radio" ng-model="value" value="firstValue">
                <input type="radio" ng-model="value" value="secondValue" > </div>

You can also observe the model change by using ng-change as follows :

In Javascript :

        $scope.newValue = function (value) {
                    alert(value);
                }

You can even watch the value by using $watch like this :

            $scope.$watch('value', function(value) {
                   alert(value);
             });

However, using ng-change is better and efficient than $watch and is easier to test.

There is no need of $watch

You can do it in more simpler way.

<body ng-controller="testCtrl">

   <label>Check me to check both: 
   <input type="checkbox" ng-model="master" ng-checked="checkTest(master)"></label><br/>

  </body>

JS :

 $scope.checkTest = function(boolChecked){

      console.log(boolChecked)
    }

Here is Plunker

You can do this in a few different ways:

with Radio Buttons

In your HTML

<input type="radio" ng-value="isButtonChecked">

In your controller

$scope.isButtonChecked = true;

with another element tag

In your HTML

<div ng-click="isButtonChecked = !isButtonChecked"></div>

In your controller

$scope.isButtonChecked = true;

本文标签: javascripthow to know if a radio button is checked or not in controller in angularjsStack Overflow