我有对象的数组,我在我的角度应用程序中使用 NG-重复
显示。我使用过滤器
和搜索输入的值过滤掉的物品。它按预期工作。但是,我有一个全选/取消选择所有选项,我只是想选择的分享范围的列表中的项目(符合当前的搜索条件的那些)。
I have an array of objects that I'm displaying in my Angular app using ng-repeat
. I'm filtering out items using filter
and the value of a search input. It works as expected. But, I have a "select all" / "deselect all" option and I only want to select the visibile items in the list (the ones that meet the current search criteria).
没有在我的控制器执行相同的逻辑(即使用的indexOf
在我的每个对象的搜索值),我怎么能告诉它通过 NG-重复 / 过滤
?
Without performing the same logic in my controller (i.e. using indexOf
the search value on each of my objects), how can I tell which items are currently filtered out by ng-repeat
/filter
?
我的观点:
<input type="text" ng-model="searchValue">
<input type="checkbox" ng-model="checkAll" ng-change="toggleAll()">
<tr ng-repeat="item in items | filter:searchValue">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
</tr>
在我的控制器的函数:
A function in my controller:
$scope.toggleAll() {
for(var i in $scope.items){
// how can I tell if this item is filtered out in the view?
}
}
我已经在这里显著简化我的code样本为简单,因为这个问题并不需要更多的细节。有没有办法做什么,我想还是需要进行一次搜索?
I have significantly simplified my code samples here for simplicity since this question doesn't need much more detail. Is there a way to do what I'm thinking or do I need to perform the "search" again?
您可以将过滤数组中的另外一个范围绑定变量,然后访问该在你的控制器。
You can bind the filtered array to another scope variable in your view, then access that in your controller.
查看:
<tr ng-repeat="item in filteredItems = (items | filter:searchValue)">
...
</tr>
控制器:
$scope.toggleAll = function () {
angular.forEach($scope.filteredItems, function (item) {
// do stuff
})
}