How to use filter in javascript instead of html

I use AngularJS and it is great. I can not find it in the documentation. What is equivalent in Javascript for this AngularJS expression:

<div>{{ (myList| filter:mySearch).length }}</div> 

Thanks for the help.

+43
angularjs
Feb 18 '13 at 12:11
source share
3 answers

In the Angular documentation, filter :

HTML template binding

{{filter_expression | filter: expression}}

In javascript

$ filter ('filter') (array, expression)

In your case, it would look like $filter('filter')(myList, mySearch) .

+75
Feb 18 '13 at 12:41
source share

As an alternative syntax, you can also directly insert a filter without going through the $filter service. For example, to add a filter filter to a controller, you can write:

 MyCtrl = function($scope, filterFilter) { $scope.filtered = filterFilter(myArray, expression); } 

A question very similar to How to use a filter in a controller?

+6
Feb 18 '13 at 13:10
source share

In your html

 <input ng-model="search"> <div ng-repeat="thing in things | filter:searchResults"></div> 

In your js

 $scope.$watch('search', function (newValue, oldValue) { var searchResults = filterFilter($scope.things, $scope.search); }); 
+1
Aug 08 '14 at 21:17
source share



All Articles