Sunday, June 23, 2013

AngularJS - Checkbox Default Value

In the following example, we set the values for our checkbox using ng-checked. The default values are set using ng-init; the same result is accomplished by initializing the value in the controller. When the user marks the checkbox as checked, we use the ng-click to change the value from true to false, or vice versa.

<div ng-init="options={};options.one.selected=true">
       <input id="option_1" type="checkbox" 
                   ng-click="options.one.selected=!options.one.selected" 
                   ng-checked="options.one.selected">
       <label for="option_1" >Option 1</label>
 </div>

Note: ng-checked expects an expression and it does not work with ng-model in the same element.


Working Example: Plunkr

1 comment: