Converting a View Filter to a Controller Filter
I am trying to optimize performance by moving code from view to controller.
I would like to move this code to my view :
ng-repeat="item in items | filter: {passed:false} | filter: data.searchQuery | filter: deepSearch() | orderBy: 'begin_datetime'"
to something like this in my controller :
$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;
}
to get this in my view :
ng-repeat="item in filteredItems | orderBy: 'begin_datetime'"
However, I am stuck with
Error: [$ injector: unpr] Unknown provider: [object Object] FilterProvider <- [object Object] Filter
Is there an easy way to do this?
Answer
Thanks to @SoluableNonagon answer, the correct syntax is:
$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;
}
source to share
you need to inject $ filter into your controller
app.controller('myController', function($filter){
Besides,
I believe filtering programmatically requires a filter name, for example:
$ scope.b = $ filter ('myFilter') ($ scope.items);
Update:
If you want to filter programmatically, and this is just a normal search, then the name of the filter will just be "filter"
$filter('filter')(array, expression, comparator) // comparator is optional
HTML equivalent:
{{ array | filter : expression : comparator}}
Example: http://plnkr.co/edit/dzPegkZaHOlHWfpVKGEV?p=preview
source to share