IOR*_*R88 23 angularjs angular-filters
我看到的过滤器很少
<tr ng-repeat="x in list | filter:search| offset:currentPage*pageSize| limitTo:pageSize ">
Run Code Online (Sandbox Code Playgroud)
在我的项目中取得好成绩,我必须在控制器中进行这种过滤而不是在视野中
我知道基本语法 $filter('filter')('x','x')但我不知道如何在控制器中创建过滤器链,所以一切都将像我的模板中的示例一样工作.
我找到了一些解决方案,现在只使用一个过滤器,但应该可以使用很多;)
$scope.data = data; //my geojson from factory//
$scope.geojson = {}; //i have to make empty object to extend it scope later with data, it is solution i found for leaflet //
$scope.geojson.data = [];
$scope.FilteredGeojson = function() {
var result = $scope.data;
if ($scope.data) {
result = $filter('limitTo')(result,10);
$scope.geojson.data = result;
console.log('success');
}
return result;
};
Run Code Online (Sandbox Code Playgroud)
我在ng-repeat工作中使用此功能很好,但我必须用几个过滤器检查它.
Ang*_*gad 25
您可以重新过滤从第一个过滤器返回的内容.等等等等.
var filtered;
filtered = $filter('filter')($scope.list, {name: $scope.filterParams.nameSearch});
filtered = $filter('orderBy')(filtered, $scope.filterParams.order);
Run Code Online (Sandbox Code Playgroud)
plunkr下面演示了上述内容.
http://plnkr.co/edit/Ej1O36aOrHoNdTMxH2vH?p=preview
Jon*_*mbo 10
除了明确地将过滤器应用于前一个结果之外,您还可以构建一个将多个过滤器链接在一起的对象.
angular.module('Demo', []);
angular.module('Demo')
.controller('DemoCtrl', function($scope, $filter) {
$scope.order = 'calories';
$scope.filteredFruits = $scope.fruits = [{ name: 'Apple', calories: 80 }, { name: 'Grapes', calories: 100 }, { name: 'Lemon', calories: 25 }, { name: 'Lime', calories: 20 }, { name: 'Peach', calories: 85 }, { name: 'Orange', calories: 75 }, { name: 'Strawberry', calories: 65 }];
$scope.filterFruits = function(){
var chain = new filterChain($scope.fruits);
$scope.filteredFruits = chain
.applyFilter('filter', [{ name: $scope.filter }])
.applyFilter('orderBy', [ $scope.order ])
.value;
};
function filterChain(value) {
this.value = value;
}
filterChain.prototype.applyFilter = function(filterName, args) {
args.unshift(this.value);
this.value = $filter(filterName).apply(undefined, args)
return this;
};
});
Run Code Online (Sandbox Code Playgroud)
<!doctype html>
<html ng-app="Demo">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>
<script src="script.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="DemoCtrl">
<input type="text" ng-model="filter" ng-change="filterFruits()" placeholder="Filter Fruits" />
<select ng-model="order">
<option value="name">name</option>
<option value="calories">calories</option>
</select>
<div ng-repeat="fruit in filteredFruits">
<strong>Name:</strong> {{fruit.name}}
<strong>Calories:</strong> {{fruit.calories}}
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26478 次 |
| 最近记录: |