In Angular one checkbox <input type="checkbox" ng-model="..."> is linked
with one model.
But in practice we usually want one model to store array of checked values
from several checkboxes.
Checklist-model solves that task without additional code in controller.
You should play with attributes of <input type="checkbox"> tag:
checklist-model instead of ng-modelchecklist-value - what should be picked as array item Please, try out demos below:
{{user.roles|json}}
<label ng-repeat="role in roles">
<input type="checkbox" checklist-model="user.roles" checklist-value="role"> {{role}}
</label>
var app = angular.module("app", ["checklist-model"]);
app.controller('Ctrl1', function($scope) {
$scope.roles = [
'guest',
'user',
'customer',
'admin'
];
$scope.user = {
roles: ['user']
};
$scope.checkAll = function() {
$scope.user.roles = angular.copy($scope.roles);
};
$scope.uncheckAll = function() {
$scope.user.roles = [];
};
$scope.checkFirst = function() {
$scope.user.roles.splice(0, $scope.user.roles.length);
$scope.user.roles.push('guest');
};
});
{{user.roles|json}}
<label ng-repeat="role in roles">
<input type="checkbox" checklist-model="user.roles" checklist-value="role.id"> {{role.text}}
</label>
var app = angular.module("app", ["checklist-model"]);
app.controller('Ctrl2', function($scope) {
$scope.roles = [
{id: 1, text: 'guest'},
{id: 2, text: 'user'},
{id: 3, text: 'customer'},
{id: 4, text: 'admin'}
];
$scope.user = {
roles: [2, 4]
};
$scope.checkAll = function() {
$scope.user.roles = $scope.roles.map(function(item) { return item.id; });
};
$scope.uncheckAll = function() {
$scope.user.roles = [];
};
$scope.checkFirst = function() {
$scope.user.roles.splice(0, $scope.user.roles.length);
$scope.user.roles.push(1);
};
});
{{user.roles|json}}
<label ng-repeat="role in roles">
<input type="checkbox" checklist-model="user.roles" checklist-value="role"> {{role.text}}
</label>
var app = angular.module("app", ["checklist-model"]);
app.controller('Ctrl3', function($scope) {
$scope.roles = [
{id: 1, text: 'guest'},
{id: 2, text: 'user'},
{id: 3, text: 'customer'},
{id: 4, text: 'admin'}
];
$scope.user = {
roles: [$scope.roles[1]]
};
$scope.checkAll = function() {
$scope.user.roles = angular.copy($scope.roles);
};
$scope.uncheckAll = function() {
$scope.user.roles = [];
};
$scope.checkFirst = function() {
$scope.user.roles.splice(0, $scope.user.roles.length);
$scope.user.roles.push($scope.roles[0]);
};
});
{{user.roles|json}}
<label ng-repeat="(key, text) in roles">
<input type="checkbox" checklist-model="user.roles" checklist-value="key"> {{text}}
</label>
var app = angular.module("app", ["checklist-model"]);
app.controller('Ctrl4', function($scope) {
$scope.roles = {
g: 'Guest',
u: 'User',
c: 'Customer',
a: 'Administrator'
};
$scope.user = {
roles: ['c']
};
$scope.checkAll = function() {
$scope.user.roles = Object.keys($scope.roles);
};
$scope.uncheckAll = function() {
$scope.user.roles = [];
};
$scope.checkFirst = function() {
$scope.user.roles.splice(0, $scope.user.roles.length);
$scope.user.roles.push('a');
};
});