AngularJS - $ timeout不是一个函数

时间:2014-12-03 12:53:19

标签: angularjs

我尝试在run函数中注入$ timeout,但是当我尝试调用它时,我得到not a function。为什么?

var mainApp = angular.module('mainApp', ['ngRoute', 'ngAnimate', 'ui.bootstrap', ngCookies']);

mainApp.run(['$rootScope', '$location', '$timeout'
        function ($rootScope, $location, $route, authService, $timeout) {
...
}]);

2 个答案:

答案 0 :(得分:15)

mainApp.run(['$rootScope', '$location', '$timeout'
        function ($rootScope, $location, $route, authService, $timeout) {
...
}]);

应该是:

mainApp.run(['$rootScope', '$location', '$route', 'authService', '$timeout',
        function ($rootScope, $location, $route, authService, $timeout) {
...
}]);

查看'数组注释'部分在这里:

https://docs.angularjs.org/api/auto/service/$injector

答案 1 :(得分:2)

当您使用依赖项名称注释函数时,外观的顺序应该匹配。

...
mainApp.run(['$rootScope', '$location', '$route', '$timeout', 'authService', 
        function ($rootScope, $location, $route, $timeout, authService) {
...
}]);