O'Reilly logo

Learning Web Development with Bootstrap and AngularJS by Stephen Radford

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Applying filters from JavaScript

There are times when you'll want to apply a filter using JavaScript, usually from your controller, so it's important we take a look at how to do this; there are a couple of options.

We can either inject the $filter service into our controller and utilize any filter included within our application. Alternatively, we can inject the filter as its own dedicated service and use it on its own. Both methods are perfectly valid, and it's down to you, whichever you prefer.

Let's first take a look at using the $filter service. We'll take the json filter we've just looked at and console.log the very same array. To begin, let's inject that service into our controller:

function AppCtl($scope, $filter){
…
}

Great! We can now utilize ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required