简体   繁体   English

AngularJS复选框过滤未在页面加载时过滤

[英]AngularJS checkbox filtering not filtering upon page load

jsfiddle here: http://jsfiddle.net/jesperlehtinen/dx05bobb/2/ jsfiddle在这里: http : //jsfiddle.net/jesperlehtinen/dx05bobb/2/


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: 要过滤的js在控制器中:

$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. 使用ng-checked="true"进行检查,但不执行实际过滤。 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? 我如何用一个值(或函数)初始化该复选框,使其既可以在页面加载时进行检查,又可以执行过滤?

I realized it was as simple as initializing the filter with the default filter value set to true. 我意识到这很简单,只需将默认过滤器值设置为true即可初始化过滤器。 So $scope.filter = {}; 因此$scope.filter = {}; becomes $scope.filter = {'Active':true}; 变成$scope.filter = {'Active':true}; .

使用ng-init将您的ng-model复选框初始化为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM