我有一个输入字段:
<input type="text" ng-model="defaultValue">
在我的控制器中,它是一个空字符串:
$scope.defaultValue = "";
我想创建一个过滤器来比较对象中的值,如:
items: [
{
repo_status: null,
cyberridge_loan_number: "FH00-0012-0003-0012",
repo_id: 18,
cr_loan_status: "CLOSED",
eff_days_delinquent: null,
principal_balance: null,
preemptive_charge_off_balance: 0,
total_final_charge_off_balance: 0
},
...
]
例如:比较repo_id&gt; 18?我创建了一个ng-repeat:
<li ng-repeat="(key, value) in data.items[0]">{{value}}</li>
(因为这个ng-repeat适用于所有相同格式的api,我使用了{key,value}对)
但如何制作逻辑过滤器(repo_id&gt; 18?)并应用于ng-repeat?
非常感谢你!
答案 0 :(得分:2)
您可以创建这样的自定义过滤器,
$scope.greaterThan = function(prop, val) {
return function(item) {
if (item[prop] > val) return true;
}
}
<强>样本强>
var app = angular.module('myApp', [])
app.controller('MyController', function($scope) {
$scope.defaultValue = 0;
$scope.items = [{
repo_status: null,
cyberridge_loan_number: "FH00-0012-0003-0012",
repo_id: 18,
cr_loan_status: "CLOSED",
eff_days_delinquent: null,
principal_balance: null,
preemptive_charge_off_balance: 0,
total_final_charge_off_balance: 0
},
{
repo_status: null,
cyberridge_loan_number: "FH00-0012-0003-0012",
repo_id: 34,
cr_loan_status: "OPEN",
eff_days_delinquent: null,
principal_balance: null,
preemptive_charge_off_balance: 0,
total_final_charge_off_balance: 0
}];
$scope.greaterThan = function(prop, val) {
return function(item) {
if (item[prop] > val) return true;
}
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyController">
<input type="text" ng-model="defaultValue">
<ul>
<li ng-repeat="item in items | filter: greaterThan('repo_id', defaultValue)"> {{item}}
</li>
</ul>
</div>
&#13;