javascript tutorial - [Solved-5 Solutions] How do we bind to list of checkbox values with AngularJS ?
- javascript - java script - javascript array
Problem:
We have a few checkboxes:
That we 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 we can bind the four checkboxes to a list in the controller?
Solution 1:
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:
And the appropriate controller code would be:
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:
And the appropriate controller code would be:
Pros: Add/remove is very easy
Cons: Somewhat more complex data structure and toggling by name is cumbersome or requires a helper method
Solution 2:
A simple solution:
Solution 3:
Here's a quick little reusable directive that seems to do what you're looking to do. I've simply called it checkList. It updates the array when the checkboxes change, and updates the checkboxes when the array changes.
Here's a controller and a view that shows how we might go about using it
(The buttons demonstrate that changing the array will also update the checkboxes.)
Finally, here is an example of the directive in action on Plunker:
Solution 4:
Solution 5:
Based on answers in this thread I've created checklist-model directive that covers all cases:
Related Searches to javascript tutorial - How do we bind to list of checkbox values with AngularJS ?
how to get value of multiple checkbox in angularjsangularjs checkbox ng-repeatangularjs checkbox exampleangularjs ng-repeat checkbox checkedmulti select dropdown with checkbox angularjsangularjs get checkbox value in controllerng-repeat checkbox ng-modelcheckbox in angularjs w3schoolshow to save checkbox value in database using angularjspost multiple checkbox value in angularjsangularjs ng-repeat checkbox checkedhow to get value of multiple checkbox in angularjs 2angularjs checkbox checked if valuemulti select dropdown with checkbox angularjsangularjs get checkbox value in controllermultiple checkbox function in angularjsjavascript tutorialjava scriptjavascriptjavascript arrayjavascript booklearn javascriptjavascript codejavascript editorjavascript classjavascripjavascript debuggerjavascript onlinejavascript examplesjavascript testjavascript documentjavascript sliderwhat is javascriptjavascript form validationjavascript validatorhtml javascriptjavascript alertjavascript eventsjavascript printjavascript domjavascript objectjavascript functionhref javascriptjavascript datejavascript promptjavascript onclickjavascript returnjavascript forjavascript numberjavascript confirmjavascript onchangejavascript regular expressionjavascript ifjavascript variablejavascript timerjavascript cookiejavascript getelementbyidjavascript innerhtmljavascript calljavascript regexpjavascript includesjavascript thisjavascript eval