O'Reilly logo

Mastering AngularJS for .NET Developers by Mohammad Wadood Majid, Golrokh Mirzaei

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

Using a filter in a controller

In the preceding section, we discussed how to create custom filters and implement them in the DOM. In this section, we will talk about how to use our custom filter inside a controller. In order to use a filter inside the controller, we need to inject the $filter dependency into the controller. This way, we will have access to both AngularJS's built-in filters and custom filters. The following code shows how to use a filter inside a controller:

var app = angular.module('myApp', []); <!-- Custom Filter -- > app.filter("ckeckCurrentYear", function () { return function (input) { var currentYear = new Date().getFullYear(); //var modelYear = input.model; if (input == currentYear) { return input + ' ' + '\u2713'; } else ...

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