9951 explained code solutions for 126 technologies


angularjsHow do I use checkboxes with AngularJS?


In order to use checkboxes with AngularJS, you can use the ng-model directive. This directive binds the value of the checkbox to a variable in the scope. For example:

<input type="checkbox" ng-model="myCheckbox">

This will bind the value of the checkbox to the myCheckbox variable in the scope. To set the initial value of the checkbox, you can use the ng-init directive. For example:

<input type="checkbox" ng-model="myCheckbox" ng-init="myCheckbox=true">

This will set the initial value of the checkbox to true.

You can also use the ng-change directive to run a function when the value of the checkbox changes. For example:

<input type="checkbox" ng-model="myCheckbox" ng-change="myFunction()">

This will run the myFunction() function whenever the value of the checkbox changes.

You can also use the ng-true-value and ng-false-value directives to set the value of the checkbox to a specific value. For example:

<input type="checkbox" ng-model="myCheckbox" ng-true-value="yes" ng-false-value="no">

This will set the value of the checkbox to yes if it is checked, and no if it is not checked.

Helpful links

Edit this code on GitHub