Inside your controller, you can watch changes to your query
text and use the Javascript equivalent of {expression} | filter: {expression}
, which is $filter('filter')
(using the filter named 'filter'
from the service $filter
, which you have to inject).
Let's say your HTML snippet is controlled by the controller MyController. Then you can change it to:
myApp.controller("MyController", ["$scope", "$filter", function($scope, $filter) {
$scope.$watch('query', function(newVal, oldVal) {
console.log("new value in filter box:", newVal);
// this is the JS equivalent of "phones | filter: newVal"
$scope.filteredArray = $filter('filter')($scope.phones, newVal);
});
}]);
Whenever your query
changes, the filtered array will be available as filteredArray
in your $scope, and you can use filteredArray
as an expression in your snippet.
All this should actually be documented in http://docs.angularjs.org/api/ng.$filter and http://docs.angularjs.org/api/ng.filter:filter. However, the documentation of the first link is way too sparse you can really only get it from the comments to the second link. I tried to add it to the docs, but after cloning angular and building the docs, accessing them via the browser simply did not work and navigating the API failed silently without any useful error, so I gave up on that.