7

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!

0

4 Answers 4

16

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]);
Sign up to request clarification or add additional context in comments.

Comments

8

You controller function (for which you currently have a function call, instead of a callback function) needs to be a function which takes $scope (controllers require $scope), also containing $filter.

controller('GraduateCalculatorController', ['$scope', '$filter', function($scope, $filter){<your code here>}]);

There are, however, two ways to use your filter. You can either inject $filter and call

$filter('slug')(inputStuff)

or you can inject slugFilter and just call it directly

slugFilter(inputStuff)

One of the most common complaints about angular is the error messages, and this seems to be the problem here!

If I wasn't clear enough on anything let me know and I'll do my best to help.

Comments

3

I think error happen when you try to define a controller and inject $filter into your app.graduateCalculator($filter).

With this way you invoke the function app.graduateCalculator($filter). But the js interpreter didn't know what is $filter, so it threw the exception.

Just update your controller into this,

.controller('GraduateCalculatorController', ['$filter', app.graduateCalculator]);

And make sure your graduateCalculator have the parameter $filter.

Comments

1

you should wrap your contorller with a function

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

2 Comments

This appears to remove the error...but my app no longer works.
True true. It would distract from the original question for me to have gone into detail why it didn't work. But I appreciate your suggestion. It led me to the answer that this parameter should be a constructor function, not a function call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.