ManjushaDC
ManjushaDC

Reputation: 190

How can I identify whether checkbox is checked or not in angularjs

I have a checkbox with ng-model assigned to it, and customized ng-true-value and ng-false-value. Is there ng-something to check that check box is checked? Also I tried using ng-checked but according to https://docs.angularjs.org/api/ng/directive/ngChecked ng-model and ng-checked should not be used together.

Upvotes: 1

Views: 4723

Answers (4)

Avinash
Avinash

Reputation: 71

there is no need of ng-checked. ng-model will have the value to true or false. 
<div ng-app="">
      <input type="checkbox" ng-model="checked"/>
      {{checked}}
</div>

please check the example in http://jsfiddle.net/19v90cnv/

Upvotes: 0

deerawan
deerawan

Reputation: 8443

You don't need to use ng-checked, ng-model is enough because it can give you false and true (if checked).

<!doctype html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Checkbox</title>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.0/angular.min.js"></script>
</head>

<body ng-app="">
  <label>Checkbox demo:
    <input type="checkbox" ng-model="master">
  </label>
  <br/> My value is: {{master}}
</body>

</html>

Here is the plunker http://plnkr.co/edit/NaTBRCeq1BeqtinEbK2t?p=preview

Upvotes: 1

Kashif Mustafa
Kashif Mustafa

Reputation: 1182

You just assign ng-model in simple input tag having type='checkbox' like

<input type="checkbox" id="your_property" ng-model="your_property" />

You will receive true and false in 'your_property'. No need of used ng-checked.

Upvotes: 1

nabinca
nabinca

Reputation: 2022

maybe you are searching for ng-if ?! Include something like this in the plunker referenced on the angular page:

<span ng-if="master">Hello, I am checked.!</span>

'master' is the variable that is bound to the check-box of the input-element.

Upvotes: 0

Related Questions