繁体   English   中英

在控制器内部注入angularjs服务会产生错误

[英]Injecting angularjs service inside a controller gives error

我试图在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 c@http://localhost:8100/static/js/external/angular/angular.min.js:30....

您要插入的每个参数都需要在数组中列出。 请参阅此处的“数组符号”: http : //docs.angularjs.org/guide/di

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM