Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to set checkbox default checked while using angularjs

Tags:

angularjs

I want to set default value of checkbox to checked ie., user must see that check box is checked when opening the page for some reason it does not work while using angularjs

here is my code

<html ng-app>

    <input type="radio" name="lookup"  ng-model="lookup" value="1" ng-checked="lookup==1" checked>Employee Lookup</input>
    <input type="radio" name="lookup"  ng-model="lookup" value="2" ng-checked="lookup==2">Company Lookup</input>
</html>
like image 547
curiosa Avatar asked Feb 19 '13 11:02

curiosa


People also ask

How do you check checkbox is checked or not in AngularJS?

The ng-checked Directive in AngularJS is used to read the checked or unchecked state of the checkbox or radio button to true or false. If the expression inside the ng-checked attribute returns true then the checkbox/radio button will be checked otherwise it will be unchecked.

How do I make check box checked?

The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.

How do you check checkbox is checked or not?

To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);


1 Answers

Since you're using ngModel and value, the radio will automatically be selected if they match. Here is the HTML and JS:

<html ng-app ng-controller="testcontroller">
  <input type="radio" ng-model="lookup" value="1">Employee Lookup</input>
  <input type="radio" ng-model="lookup" value="2">Company Lookup</input>
</html>
function testcontroller($scope){
  $scope.lookup = 1;
}

And here is a working jsFiddle demonstration: http://jsfiddle.net/BinaryMuse/ZQDts/3/ (You might have been having trouble with your jsFiddle because you misspelled ng-app.)

like image 67
Michelle Tilley Avatar answered Nov 07 '22 12:11

Michelle Tilley