How to use a filter in a controller?

sumanth Source

I have written a filter function which will return data based on the argument you are passing. I want the same functionality in my controller. Is it possible to reuse the filter function in a controller?

This is what I've tried so far:

function myCtrl($scope,filter1)
    // i simply used the filter function name, it is not working.


answered 6 years ago Prashanth Suriyanarayanan #1

Inject $filter to your controller

function myCtrl($scope, $filter)

Then wherever you want to use that filter, just use it like this:


If you want to pass arguments to that filter, do it using separate parentheses:

function myCtrl($scope, $filter)

Where arg1 is the array you want to filter on and arg2 is the object used to filter.

answered 6 years ago pkozlowski.opensource #2

Answer provided by @Prashanth is correct, but there is even easier way of doing the same. Basically instead of injecting the $filter dependency and using awkward syntax of invoking it ($filter('filtername')(arg1,arg2);) one can inject dependency being: filter name plus the Filter suffix.

Taking example from the question one could write:

function myCtrl($scope, filter1Filter) { 
  filter1Filter(input, arg1);

It should be noted that you must append Filter to the filter name, no matter what naming convention you're using: foo is referenced by calling fooFilter
fooFilter is referenced by calling fooFilterFilter

answered 5 years ago NtSpeed #3

I have another example, that I made for my process:

I get an Array with value-Description like this

states = [{
    status: '1',
    desc: '\u2713'
}, {
    status: '2',
    desc: '\u271B'

in my Filters.js:

.filter('getState', function () {
    return function (input, states) {
        for (var i = 0; i < states.length; i++) {
            if (states[i].status == input) {
                return states[i].desc;
        return '\u2718';

Then, a test var (controller):

function myCtrl($scope, $filter) {
    // ....
    var resp = $filter('getState')('1', states);
    // ....

answered 5 years ago SimplGy #4

There is another way to evaluate filters that mirrors the syntax from the views. The invocation is hairy but you could build a shortcut to it. I like that the syntax of the string is identical to what you'd have in a view. Looks like this:

function myCtrl($scope, $interpolate) { 
  $scope.$eval($interpolate( "{{ myvar * 10 | currency }} dollars." ))

answered 4 years ago aamir sajjad #5

Using following sample code we can filter array in angular controller by name. this is based on following description.

this.filteredArray = filterFilter(this.array, {name:'Igor'});


 angular.module('FilterInControllerModule', []).
    controller('FilterController', ['filterFilter', function(filterFilter) {
      this.array = [
        {name: 'Tobias'},
        {name: 'Jeff'},
        {name: 'Brian'},
        {name: 'Igor'},
        {name: 'James'},
        {name: 'Brad'}
      this.filteredArray = filterFilter(this.array, {name:'Igor'});


<!doctype html>
<html lang="en">
  <meta charset="UTF-8">
  <title>Example - example-example96-production</title>

  <script src="//"></script>
  <script src="script.js"></script>

<body ng-app="FilterInControllerModule">
    <div ng-controller="FilterController as ctrl">
      All entries:
      <span ng-repeat="entry in ctrl.array">{{}} </span>
      Filter By Name in angular controller
      <span ng-repeat="entry in ctrl.filteredArray">{{}} </span>

answered 4 years ago Ipog #6

function ngController($scope,$filter){
    $ = "aaaa";
    $scope.age = "32";

     $scope.result = function(){
        return $filter('lowercase')($;

The controller method 2nd argument name should be "$filter" then only the filter functionality will work with this example. In this example i have used the "lowercase" Filter.

answered 3 years ago Mike Gledhill #7

Here's another example of using filter in an Angular controller:

$scope.ListOfPeople = [
    { PersonID: 10, FirstName: "John", LastName: "Smith", Sex: "Male" },
    { PersonID: 11, FirstName: "James", LastName: "Last", Sex: "Male" },
    { PersonID: 12, FirstName: "Mary", LastName: "Heart", Sex: "Female" },
    { PersonID: 13, FirstName: "Sandra", LastName: "Goldsmith", Sex: "Female" },
    { PersonID: 14, FirstName: "Shaun", LastName: "Sheep", Sex: "Male" },
    { PersonID: 15, FirstName: "Nicola", LastName: "Smith", Sex: "Male" }

$scope.ListOfWomen = $scope.ListOfPeople.filter(function (person) {
    return (person.Sex == "Female");

//  This will display "There are 2 women in our list."
prompt("", "There are " + $scope.ListOfWomen.length + " women in our list.");

Simple, hey ?

answered 3 years ago Jeffrey Roosendaal #8

There are three possible ways to do this.

Let's assume you have the following simple filter, which converts a string to uppercase, with a parameter for the first character only.

app.filter('uppercase', function() {
    return function(string, firstCharOnly) {
        return (!firstCharOnly)
            ? string.toUpperCase()
            : string.charAt(0).toUpperCase() + string.slice(1);

Directly through $filter

app.controller('MyController', function($filter) {

    // HELLO
    var text = $filter('uppercase')('hello');

    // Hello
    var text = $filter('uppercase')('hello', true);


Note: this gives you access to all your filters.

Assign $filter to a variable

This option allows you to use the $filter like a function.

app.controller('MyController', function($filter) {

    var uppercaseFilter = $filter('uppercase');

    // HELLO
    var text = uppercaseFilter('hello');

    // Hello
    var text = uppercaseFilter('hello', true);


Load only a specific Filter

You can load only a specific filter by appending the filter name with Filter.

app.controller('MyController', function(uppercaseFilter) {

    // HELLO
    var text = uppercaseFilter('hello');

    // Hello
    var text = uppercaseFilter('hello', true);


Which one you use comes to personal preference, but I recommend using the third, because it's the most readable option.

answered 2 years ago Hazarapet Tunanyan #9

AngularJs lets you to use filters inside template or inside Controller, Directive etc..

in template you can use this syntax

{{ variable | MyFilter: ... : ... }}

and inside controller you can use injecting the $filter service

angular.module('MyModule').controller('MyCtrl',function($scope, $filter){
    $filter('MyFilter')(arg1, arg2);

If you need more with Demo example here is a link

AngularJs filter examples and demo

answered 2 years ago AVH #10

Simple date example using $filter in a controller would be:

var myDate = new Date();
$scope.dateAsString = $filter('date')(myDate, "yyyy-MM-dd"); 

As explained here -

answered 1 year ago pavok #11

It seems nobody has mentioned that you can use a function as arg2 in $filter('filtername')(arg1,arg2);

For example:

$scope.filteredItems = $filter('filter')(items, function(item){return item.Price>50;});

answered 10 months ago Amay Kulkarni #12

Use below code if we want to add multiple conditions, instead of single value in javascript angular filter:

var modifiedArray = $filter('filter')(array,function(item){return (item.ColumnName == 'Value1' || item.ColumnName == 'Value2');},true)

answered 7 months ago serdarsenay #13

First of all inject $filter to your controller, making sure ngSanitize is loaded within your app, later within the controller usage is as follows:

$filter('linky')(text, target, attributes)

Always check out the angularjs docs

comments powered by Disqus