我不知道如何编写这个过滤表达式。
我有一系列文档:
[{title: 'doc1', read: false}
{title: 'doc2', read: false}
{title: 'doc3', read: false}
{title: 'doc4', read: true}
{title: 'doc5', read: false}]
我的观点
<input type="checkbox" ng-model="filterRead">
<div ng-repeat="doc in documents track by $index | filter: ??? ">
{{doc.title}}
</div>
如果 checkbox/filterRead 为 true,则不指定任何过滤器并显示所有文档。如果该复选框为 false,则仅显示 doc.read=false 的文档。我不知道如何优雅地编写这个过滤表达式。
最佳答案
根据你的逻辑,你不需要过滤器,你可以使用 ng-if:
Controller :(或者如果您希望直接在 HTML 中使用逻辑)
$scope.showDoc = function () {
return $scope.filterRead || !doc.read;
}
和 HTML:
<div ng-repeat="doc in documents track by $index" ng-if="showDoc()">
{{doc.title}}
</div>
关于javascript - AngularJS ng-repeat 和切换过滤器开/关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30200372/