Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angularjs运行命名函数_Javascript_Angularjs - Fatal编程技术网

Javascript angularjs运行命名函数

Javascript angularjs运行命名函数,javascript,angularjs,Javascript,Angularjs,这有什么问题 (function() { 'use strict'; angular .module('app') .run('pageTitle', pageTitle); function pageTitle($rootScope, $http) { $rootScope.$on('$routeChangeSuccess', function (event, current, previous) {

这有什么问题

(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;
        });
    }

})();
我犯了这个错误

错误:ng:areq

错误的论点

参数“fn”不是函数,获取字符串。run()接受一个参数,并且它必须是函数,因此请删除第一个字符串

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

根据Moncef Hassein bey的回答,
.run()
只接受一个参数

您还需要将
$rootScope
$http
注入到函数中,否则您将面临进一步的问题。在
页面标题
函数上方放置以下行:

pageTitle.$inject = ['$rootScope', '$http'];

谢谢你的建议。我应该为每个函数都这样做吗?仅当函数需要依赖项注入时。如果没有向函数传递任何依赖项,则无需
$inject
任何操作。只是澄清一下,这只适用于控制器、服务、工厂等中使用的命名函数。