下面是如何使用ng-repeat
和自定義過濾器來做到這一點。只需使用您綁定的值並替換它們爲min
和max
在myfilter
,如:
<div ng-repeat="item in items | myfilter: { min: slider.min, max: slider.max }">
JSFiddle
HTML:
<div ng-app="app" ng-controller="dummy">
<div ng-repeat="item in items | myfilter: { min: 185, max: 500 }">
<p>{{item.name}}</p>
</div>
</div>
JS:
var app = angular.module("app", []);
app.controller('dummy', function($scope, $sce) {
$scope.items = [{name: 'hello', cost: 100}, {name: 'world', cost: 200}]
});
app.filter('myfilter', function() {
return function(items, condition) {
var filtered = [];
if(condition === undefined || condition === ''){
return items;
}
angular.forEach(items, function(item) {
if(item.cost >= condition.min && item.cost <= condition.max){
filtered.push(item);
}
});
return filtered;
};
});
謝謝!有效 : ) –