我是Angularjs的新手。我们可以按多个键值进行过滤,但在这里我需要按单值过滤,但我的父数据包含多个值。 像
在控制器中:
$scope.movies = [{name:'aa', criteria:'SECURITY TYPE:Stocks|GEOGRAPHY:US'},
{name:'bb', criteria:'TRADING STRATEGY:Monthly|YIELD:Mid,Blend'},
{name:'cc', criteria:'SECURITY TYPE:Stocks|YIELD:Mid,Blend'},
{name:'dd', criteria:'MARKETCAPITALIZATION:Mega|VOLATILITY:Low'}];
$scope.filterByProperties = function (data) {
return $scope.filter[data.criteria] || noSubFilter($scope.filter);
};
function noSubFilter(subFilterObj) {
for (var key in subFilterObj) {
if (subFilterObj[key]) return false;
}
return true;
}
在查看文件中:
<label>SECURITY TYPE</label>
<input type="checkbox" ng-model="filter['Stocks']"> Stocks
<label>Yield</label>
<input type="checkbox" ng-model="filter['Mid']"> Mid
但是,由于data.criteria字段包含多个值,我将如何从复选框中过滤它们?
答案 0 :(得分:0)
使用自定义过滤器过滤掉数据。
<label>SECURITY TYPE</label>
<input type="checkbox" ng-model="criteria.stocks"> Stocks</input>
<label>Yield</label>
<input type="checkbox" ng-model="criteria.mids"> Mid</input>
<ul>
<li ng-repeat="m in movies | typeFilter: criteria">
{{m.name}} {{m.criteria}}
</li>
添加过滤器
.filter('typeFilter', function($filter) {
return function(input, criteria) {
console.log(criteria);
console.log(input);
var data = input;
if(criteria && !criteria.mids && !criteria.stocks){
return input;
}
if(criteria.stocks){
console.log(data);
data = $filter('filter')( data, {criteria:'Stocks'} );
console.log(data);
}
if(criteria.mids){
console.log(data);
data = $filter('filter')(data, {criteria:'Mid'} );
console.log(data);
}
return data;}});
我在这里创建了一个工作示例http://codepen.io/mkl/pen/wGOKJo