0

What is wrong with this?

(function() {
    'use strict';

    angular
        .module('app')
        .run('pageTitle', pageTitle);

    function pageTitle($rootScope, $http) {
        $rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
            $rootScope.title = current.$$route.title;
        });
    }

})();

I´m getting this error

Error: ng:areq

Bad Argument

Argument 'fn' is not a function, got string

2 Answers 2

1

As per Moncef Hassein-bey's answer, .run() only accepts one argument.

You also need to inject $rootScope and $http into your function or you will face further issues. Place the following line above your pageTitle function:

pageTitle.$inject = ['$rootScope', '$http'];
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the advice. should I do this for every function?
Only where the function requires dependency injection. If you are not passing any dependencies to the function then there is no need to $inject anything. Just to clarify, this only applies to named functions you use in your Controller, Service, Factory etc.
1

.run() accepts one argument, and it must be a function, so remove the first string.

angular.module('app').run(pageTitle);

Comments

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.