AngularJS: API: ng/directive/input[checkbox]


input[checkbox]

  1. - input in module ng

HTML checkbox.

Directive Info

  • This directive executes at priority level 0.

Usage

<input type="checkbox"
       ng-model=""
       [name=""]
       [ng-true-value=""]
       [ng-false-value=""]
       [ng-change=""]>

Arguments

ParamTypeDetails
ngModelstring

Assignable angular expression to data-bind to.

name
(optional)
string

Property name of the form under which the control is published.

ngTrueValue
(optional)
expression

The value to which the expression should be set when selected.

ngFalseValue
(optional)
expression

The value to which the expression should be set when not selected.

ngChange
(optional)
string

Angular expression to be executed when input changes due to user interaction with the input element.

Example

index.html
<script>
  angular.module('checkboxExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.value1 = true;
      $scope.value2 = 'YES'
    }]);</script><form name="myForm" ng-controller="ExampleController">
  Value1: <input type="checkbox" ng-model="value1"> <br/>
  Value2: <input type="checkbox" ng-model="value2"
                 ng-true-value="'YES'" ng-false-value="'NO'"> <br/>
  <tt>value1 = {{value1}}</tt><br/>
  <tt>value2 = {{value2}}</tt><br/>
 </form>
protractor.js
it('should change state', function() {
  var value1 = element(by.binding('value1'));
  var value2 = element(by.binding('value2'));

  expect(value1.getText()).toContain('true');
  expect(value2.getText()).toContain('YES');

  element(by.model('value1')).click();
  element(by.model('value2')).click();

  expect(value1.getText()).toContain('false');
  expect(value2.getText()).toContain('NO');});