2016-06-07 166 views
1

我是AngularJS的新手,我似乎無法弄清楚我的服務注入出了什麼問題。AngularJS中的服務注入

服務 - ErrorTranslatorForApiCalls.js

(function() { 
    var errorTranslatorModule = angular.module('exceptionHandler'); 

    errorTranslatorModule.factory('apiStatusTranslator', function() { 
     var translateStatusToFriendlyMessage = function (status) { 

      var friendlyMessage = "Error occured while performing the action"; 

      switch (status) { 
      case 401: 
       friendlyMessage = "You are unauthorized to perform this action"; 
       break; 
      } 

      return friendlyMessage; 
     }; 

     return { 
      translateStatusToFriendlyMessage : translateStatusToFriendlyMessage 
     }; 
    }); 
}); 

這裏是我注入了 - GroupSearch.js

(function() { 

    agGrid.initialiseAgGridWithAngular1(angular); 

    var module = angular.module('groupEditor', ["ngRoute", "ngResource", "agGrid", "exceptionHandler"]); 

    module.constant('BASEURL', 'http://localhost/XXX.XXX.XXX/api/group/'); 
    module.controller('SearchController', ['$scope', '$resource', 'BASEURL', "apiStatusTranslator", function ($scope, $resource, BASEURL, apiStatusTranslator) { ............ REST OF CODE GOES HERE; 

我的HTML的JavaScript包括

<script src="Scripts/angular.js"></script> 
<script src="Scripts/angular-resource.min.js"></script> 
<script src="Scripts/angular-route.js"></script> 
<script src="Scripts/ag-grid.min.js"></script> 
<script type="text/javascript" src="Services/ErrorTranslatorForApiCalls.js"></script> 
<script type="text/javascript" src="Controllers/GroupSearch.js"></script> 
+0

爲什麼你把你的服務包裝到一個單獨的模塊中,我不會那樣做一個應用程序。但無論如何,你的代碼看起來很好。它在控制檯輸出中說了什麼?什麼是'agGrid.initialiseAgGridWithAngular1(angular);'? –

+0

我認爲你需要在模塊聲明中注入一次。嘗試注入errortrans .... –

+0

我錯過* ErrorTranslatorForApiCalls.js * –

回答

0

根據用戶的建議,控制檯錯誤打印在這裏會有所幫助。但是,如果您在代碼塊中放置的是您的代碼,則會出現錯誤。您在控制器代碼中最後缺少「])」。

module.controller('SearchController', ['$scope', '$resource', 'BASEURL', "apiStatusTranslator", function ($scope, $resource, BASEURL, apiStatusTranslator) { } 

])//This is missing