2014-12-03 59 views
4

我試圖在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) { 
... 
}]); 
+1

這段代碼有很多語法錯誤。糾正它,然後再試一次 – simon 2014-12-03 12:59:12

回答

14
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

2

當你註釋功能具有依賴性的名稱,順序外觀應該匹配。

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