我有一个ng-repeat使用这样的过滤器:
#1 <div class="contestReports" ng-repeat="contest in contests | filter:{votingOver:true}"> <contestreport></contestreport> </div>
我想让客户能够过滤它,所以我已经将过滤器分配给这样的变量:
#2 <div ng-init="reportFilter = {votingOver:true}"></div> <div class="contestReports" ng-repeat="contest in contests | filter:reportFilter"> <contestreport></contestreport> </div>
您是否也尝试包装ng-init
原文链接:https://www.f2er.com/angularjs/141759.html<div ng-init="(reportFilter = '{votingOver:true}')"></div>
但是,正如我在之前的评论中所述 – angularjs documentation表示在大多数情况下使用ngInit是一种不好的做法.因此,如果可能,这不应该是您的问题的解决方案.
你的#2代码实际上有效,请检查这个插件 – http://plnkr.co/edit/dBDyYPd3ZoUVdXngu52t?p=preview
//html <div ng-init="reportFilter = {votingOver:false}"></div> <div class="contestReports" ng-repeat="contest in contests | filter:reportFilter"> {{contest | json}} </div> </div> //js in controller $scope.contests = [ {id:1,title:'1',votingOver:false},{id:2,title:'2',votingOver:true},{id:3,title:'3',votingOver:true} ];