2014-03-05 83 views
0

我想在Angularjs控制器中注入一個Angularjs服務,但它給了我錯誤。在控制器內注入angularjs服務給出錯誤

service.js

angular.module('gbuyRef',[]).factory('globalDealListService'.function(){ 
    var globalDealList = {}; 

    return { 
     getGlobalDealList: function() { 
     return globalDealList; 
     }, 
     setGlobalDealList: function (value) { 
     globalDealList = value; 
     } 
    }; 

}) 

Controller.js

angular.module('gbuyRef').controller('HomeController',['$scope', 'globalDealListService', function ($scope,$window,$http,$cookies,globalDealListService) { 

    $scope.logOut = function() { 
     $http({ 
      method : 'POST', 
      url : '/logOut', 
      headers : { 'Content-Type': 'application/x-www-form-urlencoded' } 
      // set the headers so angular passing info as form data (not request payload) 
      }).success(function(data, status, headers, config) { 
      console.log($cookies.globalProductList) 

      $window.location.href="/static/html/login.html" 
      }).error(function(data, status, headers, config) { 
      $scope.status = status; 
      $window.alert("error") 
     }); 
    } 
}]); 

能有一個人告訴我什麼,我做錯了什麼?我得到在Firebug下面的錯誤。

Error: [$injector:unpr] http://errors.angularjs.org/1.2.11/$injector/unpr?p0=globalDealListServiceProvider%20%3C-%20globalDealListService t/<@http://localhost:8100/static/js/external/angular/angular.min.js:6 Yb/l.$injector<@http://localhost:8100/static/js/external/angular/angular.min.js:32 [email protected]://localhost:8100/static/js/external/angular/angular.min.js:30.... 
+2

'[ '$範圍', 'globalDealListService',函數($範圍,$窗口,$ HTTP,$餅乾,globalDealListService)'這些列表不匹配1 – hassassin

+0

感謝@hassassin,那麼如何匹配呢? – tushR

+0

使用angular.js代替angular.min.js以獲得更多可讀性的angular調試。你會看到注入鏈 – lib3d

回答

0

您正在被注入的每個參數都需要在數組中列出。請參閱 「數組符號」 在這裏:http://docs.angularjs.org/guide/di

.controller('HomeController',['$scope', '$window', '$http', '$cookies', 'globalDealListService', 
    function ($scope,$window,$http,$cookies,globalDealListService) { 
     ... 
    } 
+0

仍然出現error.Error:[$ injector:unpr] http://errors.angularjs.org/1.2.11/$injector/unpr?p0=globalDealListServiceProvider%20%3C-%20globalDealListService t/<@http:// localhost:8100/static/js/external/angular/angular.min.js:6 Yb/l。$ injector <@http:// localhost:8100/static/js/external/angular/angular .min.js:32 c @ http:// localhost:8100/static/js/external/angular/angular.min.js:30 – tushR

+0

第一:你的文件真的存在嗎? .. chrome - > f12 - > ctrl + o搜索你的文件'service.js' // second:evaluate in console'angular.injector(['ng','gbuyRef'])。has('globalDealListService' )'你會看到你是否真的在gbuyRef模塊上註冊了這個服務 – lib3d

+0

是的,它工作,頁面被緩存並返回錯誤。 – tushR

相關問題