Check All , UnCheck All - AngularJs

Code to check and uncheck all using angularjs

by Vincent Audebert

HTML

<div ng-controller="checkboxController">
    <div>Check All
        <input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" />
    </div>
    <ul>
        <li ng-repeat="item in items">
            <label>{{item.name}}
                <input type="checkbox" ng-model="item.selected" />
            </label>
        </li>
    </ul>
</div>

JavaScript

angular.module("CheckAllModule", [])
    .controller("checkboxController", function checkboxController($scope) {
    $scope.selectedAll = false;

    $scope.items = [{
        name: "Item one"
    }, {
        name: "Item two"
    }, {
        name: "Item three"
    }];
    $scope.checkAll = function () {
        if (!$scope.selectedAll) {
            $scope.selectedAll = true;
        } else {
            $scope.selectedAll = false;
        }
        angular.forEach($scope.items, function (item) {
            item.selected = $scope.selectedAll;
        });

    };


});