Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind to list of checkbox values with AngularJS?

I have a few checkboxes:

<input type='checkbox' value="apple" checked> <input type='checkbox' value="orange"> <input type='checkbox' value="pear" checked> <input type='checkbox' value="naartjie"> 

That I would like to bind to a list in my controller such that whenever a checkbox is changed the controller maintains a list of all the checked values, for example, ['apple', 'pear'].

ng-model seems to only be able to bind the value of one single checkbox to a variable in the controller.

Is there another way to do it so that I can bind the four checkboxes to a list in the controller?

like image 888
nickponline Avatar asked Jan 25 '13 02:01

nickponline


People also ask

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

Just define an ng-model directive in the checkbox and to find checkbox checked or not check model return value (TRUE or FALSE). If it has TRUE means checkbox is been checked.

Is checked 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.

What is Ng-checked?

Definition and Usage. The ng-checked directive sets the checked attribute of a checkbox or a radiobutton. The checkbox, or radiobutton, will be checked if the expression inside the ng-checked attribute returns true. The ng-checked directive is necessary to be able to shift the value between true and false .

What is the use of NG change in AngularJS?

AngularJS ng-change Directive The ng-change directive tells AngularJS what to do when the value of an HTML element changes. The ng-change directive requires a ng-model directive to be present.


1 Answers

There are two ways to approach this problem. Either use a simple array or an array of objects. Each solution has it pros and cons. Below you'll find one for each case.


With a simple array as input data

The HTML could look like:

<label ng-repeat="fruitName in fruits">   <input     type="checkbox"     name="selectedFruits[]"     value="{{fruitName}}"     ng-checked="selection.indexOf(fruitName) > -1"     ng-click="toggleSelection(fruitName)"   > {{fruitName}} </label> 

And the appropriate controller code would be:

app.controller('SimpleArrayCtrl', ['$scope', function SimpleArrayCtrl($scope) {    // Fruits   $scope.fruits = ['apple', 'orange', 'pear', 'naartjie'];    // Selected fruits   $scope.selection = ['apple', 'pear'];    // Toggle selection for a given fruit by name   $scope.toggleSelection = function toggleSelection(fruitName) {     var idx = $scope.selection.indexOf(fruitName);      // Is currently selected     if (idx > -1) {       $scope.selection.splice(idx, 1);     }      // Is newly selected     else {       $scope.selection.push(fruitName);     }   }; }]); 

Pros: Simple data structure and toggling by name is easy to handle

Cons: Add/remove is cumbersome as two lists (the input and selection) have to be managed


With an object array as input data

The HTML could look like:

<label ng-repeat="fruit in fruits">   <!--     - Use `value="{{fruit.name}}"` to give the input a real value, in case the form gets submitted       traditionally      - Use `ng-checked="fruit.selected"` to have the checkbox checked based on some angular expression       (no two-way-data-binding)      - Use `ng-model="fruit.selected"` to utilize two-way-data-binding. Note that `.selected`       is arbitrary. The property name could be anything and will be created on the object if not present.   -->   <input     type="checkbox"     name="selectedFruits[]"     value="{{fruit.name}}"     ng-model="fruit.selected"   > {{fruit.name}} </label> 

And the appropriate controller code would be:

app.controller('ObjectArrayCtrl', ['$scope', 'filterFilter', function ObjectArrayCtrl($scope, filterFilter) {    // Fruits   $scope.fruits = [     { name: 'apple',    selected: true },     { name: 'orange',   selected: false },     { name: 'pear',     selected: true },     { name: 'naartjie', selected: false }   ];    // Selected fruits   $scope.selection = [];    // Helper method to get selected fruits   $scope.selectedFruits = function selectedFruits() {     return filterFilter($scope.fruits, { selected: true });   };    // Watch fruits for changes   $scope.$watch('fruits|filter:{selected:true}', function (nv) {     $scope.selection = nv.map(function (fruit) {       return fruit.name;     });   }, true); }]); 

Pros: Add/remove is very easy

Cons: Somewhat more complex data structure and toggling by name is cumbersome or requires a helper method


Demo: http://jsbin.com/ImAqUC/1/

like image 156
Yoshi Avatar answered Sep 23 '22 19:09

Yoshi