我如何动态地在ng-repeat上设置filter属性?
这是我的模板......
<div class="list-group">
<div ng-repeat="article in articles | activeFilter">
<a href="#" class="list-group-item">
<h3 class="list-group-item-heading">{{ article.title }}</h3>
<h4 class="list-group-item-text">{{ article.author }}</h4>
<p class="list-group-item-text">"{{ article.blurb }}"</p>
</a>
</div>
</div>
其中“activeFilter”是我想通过控制器设置的属性......
...
$scope.activeFilter = 'someFilterType'
...
过滤器看起来像这样......
.filter('someFilterType', function () {
return function (items) {
var rv = [];
for (var p in items) {
if (items[p].myFilterProp === false)
rv.push(items[p]);
}
return rv;
}
})
我认为我可以用这种方式动态改变ng-repeat的过滤器,但它似乎没有用,我不知道为什么。
答案 0 :(得分:2)
尝试这样的事情:
HTML
ng-repeat="article in articles | filter:activeFilter[filter]"
控制器:
$scope.filter = "name1";
$scope.activeFilter = {
name1: $scope.someFilterFunction,
name2: $scope.someOtherFilter,
name3: $scope.andAnother
};
$scope.someFilterFunction = function() {
var rv = [];
for (var p in items) {
if (items[p].myFilterProp === false)
rv.push(items[p]);
}
return rv;
};