AngularJS: Select All Checkboxes

    There were very few examples I found on this idea however most of them were not complete or depended on libraries were not included in my projects. I could simply include them and call it a day but that would be too easy. So I took the best example I could find and rewrote it in just AngularJS.

Code:
$scope.items = [ {item:1, flag: true}, {item:2, flag: true}, {item:3, flag: true }];

$scope.allNeedsClicked = function () {
    var newValue = !$scope.allNeedsMet();
    for (var i = 0; i < $scope.items.length; i++) {
        $scope.items[i].flag = newValue;
    }
};

$scope.allNeedsMet = function () {
    var needsMet = [];
    for (var i = 0; i < $scope.items.length; i++) {
        if ($scope.items[i].flag === true)
            needsMet.push(true);
    }
    console.log($scope.items.length, needsMet.length);
    return (needsMet.length === $scope.items.length);
};


Solution: https://fassetar.github.io/blog-examples/angularjs-select-all-checkboxes/

Notes
I should probably put this in a directive rather than a controller, since I would this in a lot of place.

Resources
Plunker - Best solution in my opinion.
html - javascript select all checkboxes in a table - Stack Overflow
javascript - Jquery "select all" checkbox - Stack Overflow - close but not complete, check all then toggle a item. Notice that the All doesn't untoggle? 

Popular posts from this blog

Aspetcore: NLog with Postgresql

Mongodb Cheatsheet

Installing Windows on Acer chromebook 15 cb3-532