Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - $filter is not defined

I am trying to inject a filter into my controller and use it as such:

angular
    .module('graduateCalculator', [])
        .filter('slug', function() {
                return function(input) {
                    if(input) {
                        return input.toLowerCase().replace(/[^a-z-]/g, '-');
                    }
                };
        }).controller('GraduateCalculatorController', ['$filter', app.graduateCalculator($filter)]);

However, I get the above error. I am obviously doing something very wrong - just not obvious enough to me.

Uncaught ReferenceError: $filter is not defined

It may have something to do with my function. It's written like so:

var app = {

    graduateCalculator: function($filter) {
        window.console.log( $filter('slug')('A random looking string...') );
    }

};

Help appreciated!

like image 653
Michael Giovanni Pumo Avatar asked Aug 13 '15 11:08

Michael Giovanni Pumo


1 Answers

The 2nd parameter to [module].controller is a constructor function, not calling the constructor function...

// change this...
.controller('GraduateCalculatorController', ['$filter', app.graduateCalculator($filter)]);
// to this...
.controller('GraduateCalculatorController', ['$filter', app.graduateCalculator]);
like image 178
Brocco Avatar answered Sep 21 '22 11:09

Brocco