2014-04-17 46 views
15

好吧我現在已經正式禿頂了,因爲這個臭名昭着的問題讓我的頭髮變得模糊:最後的AngularJS應用程序不起作用,帶着這個錯誤:AngularJS錯誤:未知的提供者:aProvider < - a

Error: [$injector:unpr] Unknown provider: aProvider <- a http://errors.angularjs.org/1.2.6/ $injector/unpr?p0=aProvider%20%3C-%20a at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:11492 at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26946 at Object.c [as get] (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26250) at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:27041 at c (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26250) at Object.d [as invoke] (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26496) at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:9:910 at Object.f [as forEach] (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:11927) at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:9:856 at j (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:5:27235)

很多其他人也有這個問題爲好,但看起來像它可以通過聲明的依賴性被固定爲一個數組,而不是裸函數的參數,就像這樣:

angular.module('my-app').controller('LoginCtrl', [ '$scope', 'HttpService', function($scope, HttpService) { ... }]); 

,而不是這樣的:

angular.module('my-app').controller('LoginCtrl', function($scope, HttpService) { ... }); 

但它不適用於我的情況。我檢查了我的所有腳本(咖啡和生成的javascripts),它們都使用適當的數組式樣聲明。

這個問題顯然不是來自額外的軟件包。我嘗試將所有額外的軟件包引用移出<!-- bower:js -->塊(這樣它們不會被grunt壓縮),但問題仍然存在。這意味着,這是我的代碼責怪...但是,再次,我試過(似乎是)只有修復可用,無濟於事。

任何提示,即使在如何正確調試此?

提前致謝!

+0

您可以禁用縮小,看看哪個供應商拋出此錯誤。 – Chandermani

+0

那麼,如果我禁用縮小,只是與'grunt serve'一起去,沒有錯誤拋出 - 該應用程序只是完美的作品。 –

+1

一個接一個地禁用你的模塊,以便檢測出你的代碼中存在錯誤的部分。某處有你的DI錯誤。 –

回答

27

最後我發現了這個問題。是的,這是我錯過的DI錯誤。

對於所有可能遭受同樣頭痛的人:數組格式聲明也必須在$routeProviderresolve選項中完成。在我的情況下(CoffeeScript提前):

app.config (['$routeProvider', ($routeProvider) -> 
    $routeProvider 
    .when '/', 
     templateUrl: 'views/main.html' 
     controller: 'MainCtrl' 
     resolve: 
     groups: ['GroupService', (GroupService) -> # I MISSED THIS 
      return GroupService.getAll() 
     ] 
     entries: ['EntryService', (EntryService) -> # AND THIS 
      return EntryService.getAll() 
     ] 
    # ... 
]) 

希望這有助於!

+0

你可以請發佈生成的JS? –

+0

我花了一整天的時間,試圖解決一個問題,最終,問題就像這樣簡單。 –

+0

我不能滿足這個。 –

3

如果您使用隱式注入而不是顯式聲明您的依賴關係,則可能出現此行爲。根據我的經驗,我遇到了這種類型的問題,它帶有返回可實例化類的特定類型的Angular.js服務(例如創建抽象控制器類或其他特定情況)。

例如:AbstractBaseControllerClass

在縮小我有同樣的問題。我解決了使用依賴注入的內部聲明。 希望這可以幫助

+0

你救了我的命。我花了這麼多的時間,解決方案是使用我的提供者之一的內部聲明! –

0

對於那些不喜歡CoffeeScript的人。

我只是把我的一些代碼,並把它。

$stateProvider 

    .state('screen', { 
     url: '/user/', 
     templateUrl: 'user.html', 
     controller: 'UserController', 
     controllerAs: 'user', 
     location: "User", 
     resolve: ['$q', 'UserService', '$state', '$timeout', authenticateUser 
     ] 
    }) 

function authenticateUser($q, UserService, $state, $timeout) { 

    UserService.getLoginStatus().then(function() { 
     if (UserService.user) { 
      console.log("is user"); 
      return $q.when(); 
     } else { 
      console.log("not user"); 

      $timeout(function() { 
       // This code runs after the authentication promise has been rejected. 
       // Go to the log-in page 
       $state.go('login') 
      }); 
      return $q.reject() 

     } 
    }); 

} 
相關問題