Calling Filters in Your Angular Controller

Here is a quick tip for how to call a filter from within your Angular controller. This example assumes that you already know what a filter is and have one created.

  1. Inject $filter into your controller

     angular.module('sample').controller('SampleController', SampleController);
     /* @ngInject */
     function SampleController($filter) { 
  2. Call your filter by calling $filter(“filter name”)(arg1, arg2, arg3).

    • So in the view you would call your filter called myFilter on myDateVariable with arguments arg1 and arg2, you would use:

        <p>{{myDateVariable | myfilter : arg1 : 'arg2' }}</p>
    • To call the same filter from within your controller:

        function SampleController($filter) { 
            var value = $filter("myFilter")(myDateVariable, arg1, arg2);

Dialogue & Discussion

back to top 