【发布时间】:2014-08-03 04:24:19
【问题描述】:
我有一个关于如何禁用与复选框相关的 AngularJS 过滤器的问题。
我有一个经典的结构:
对象:
$scope.wines = [
{ "name": "Wine a", "type": "red", "country": "france", "style": "strong" },
{ "name": "Wine a", "type": "white", "country": "italie", "style": "medium" },
{ "name": "Wine a", "type": "white", "country": "france", "style": "light" },
{ "name": "Wine a", "type": "rose", "country": "usa", "style": "strong" },
{ "name": "Wine a", "type": "champagne", "country": "chili", "style": "medium" },
{ "name": "Wine a", "type": "red", "country": "brazil", "style": "strong" },
{ "name": "Wine a", "type": "red", "country": "france", "style": "strong" }
];
$scope.winetypes = {red : true,white : true,rose : true, champagne : true};
此过滤器用于仅显示使用复选框做出的相关选择:
app.filter('winetypefilter', function () {
return function(input, filter) {
var result = [];
angular.forEach(input, function (wine) {
angular.forEach(filter, function (isfiltered, type) {
if (isfiltered && type === wine.type) {
result.push(wine);
}
});
});
return result;
};
结果用这个 html 代码显示:
<li ng-repeat="(type, value) in winetypes">
<input type="checkbox" ng-model="winetypes[type]" /> {{type}}
</li>
<ul>
<li ng-repeat="wine in wines | winetypefilter:winetypes">
{{wine.name}} is a {{wine.type}} with {{wine.style}} style from {{wine.country}}
</li>
</ul>
- 当所有葡萄酒复选框都未选中 (false) 时,我会禁用 winetypefilter,以在页面上显示所有结果。我试过没有成功!!
【问题讨论】:
-
基本上你希望在所有复选框都未选中时显示所有项目?
-
是的,没错!!很好的帮助,非常感谢!
标签: angularjs checkbox filter angularjs-ng-repeat