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

时间:2016-04-15 09:29:55

标签: javascript angularjs checkbox filter

jsfiddle here:http://jsfiddle.net/jesperlehtinen/dx05bobb/2/

我有一个对象列表,我想在页面加载时过滤。基本上我有一个像这样的复选框:

var session: MCOIMAPSession = MCOIMAPSession() session.hostname = "imap.gmail.com" session.port = 993 session.username = UICKeyChainStore.stringForKey("username") session.password = UICKeyChainStore.stringForKey("password") session.connectionType = MCOConnectionTypeTLS var operation: MCOIMAPFetchContentOperation = session.fetchMessageByUIDOperationWithFolder("INBOX", uid: message.uid) operation.start({(error: NSError, data: NSData) -> Void in var messageParser: MCOMessageParser = MCOMessageParser(data: data) var msgHTMLBody: String = messageParser.htmlBodyRendering() webView.loadHTMLString(msgHTMLBody, baseURL: nil) })

和项目清单:

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

要过滤的js在控制器中:

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

现在这个有用了。当我加载页面时,取消选中该复选框,并且签入将执行过滤。但是,我希望在加载时检查复选框,并进行过滤。使用$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; } 会对其进行检查,但它不会执行实际过滤。如何使用值(或函数)初始化复选框,使其在页面加载时都被检查并执行过滤?

2 个答案:

答案 0 :(得分:2)

我意识到它就像初始化过滤器一样简单,默认过滤器值设置为true。因此$scope.filter = {};变为$scope.filter = {'Active':true};

答案 1 :(得分:0)

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