我正在尝试通过将代码从视图移动到控制器来优化性能。
我想在视图:
中移动此代码ng-repeat="item in items | filter: {passed:false} | filter: data.searchQuery | filter: deepSearch() | orderBy: 'begin_datetime'"
在我的控制器中
: $scope.filteredItems = [];
var applyFilters = function() {
var items = [];
items = $filter({passed:false})($scope.items);
items = $filter($scope.data.searchQuery)(items);
items = $filter($scope.deepSearch())(items);
$scope.filteredItems = items;
}
要在视图中获取此内容:
ng-repeat="item in filteredItems | orderBy: 'begin_datetime'"
然而,我陷入了
错误:[$ injector:unpr]未知提供者:[object Object] FilterProvider< - [object Object] Filter
有没有一种简单的方法可以做到这一点?
感谢@SoluableNonagon的回答,正确的语法是:
$scope.filteredItems = [];
var applyFilters = function() {
var items = [];
items = $filter('filter')($scope.items, {passed:false});
items = $filter('filter')(items, $scope.data.searchQuery);
items = $filter('filter')(items, $scope.deepSearch());
$scope.filteredItems = items;
}
答案 0 :(得分:2)
你必须在控制器中注入$ filter
app.controller('myController', function($filter){
此外,
我认为以编程方式过滤需要一个过滤器名称,如下所示:
$ scope.b = $ filter('myFilter')($ scope.items);
更新:
如果您想以编程方式进行过滤,并且只是常规搜索,那么过滤器名称将只是“过滤器”
$filter('filter')(array, expression, comparator) // comparator is optional
HTML等价物是:
{{ array | filter : expression : comparator}}