vendredi 15 avril 2016

AngularJS checkbox filtering not filtering upon page load

I have a list of objects which I would like to filter on page load. Basically I have a checkbox like this:

<input type="checkbox" class="form__checkbox" ng-model="filter['processed']">

and a list of items:

<tr data-ng-repeat="card in cards.data | filter:filterByStatus">

the js to filter is in the controller:

$scope.cards = $scope.$parent.cards;

$scope.filter = {};

$scope.filterByStatus = function(card) {
    return $scope.filter[card.status] || noFilter($scope.filter);
};

function noFilter(filterObj){
    for(var key in filterObj){
        if(filterObj[key]){
            return false;
        }
    }
    return true;
}

Right now this works, kind of. When I load the page the checkbox is unchecked, and checking in performs the filtering. However, I would like the checkbox to be checked on load, and also do the filtering. Using ng-checked="true" makes it checked, but it doesn't perform the actual filtering. How could I initialize the checkbox with a value (or function) that makes it both be checked on page load and also perform the filtering?




Aucun commentaire:

Enregistrer un commentaire