2016-12-22 119 views
0

我只想問我的代碼有什麼問題嗎?Angularjs:服務未定義

指數:

// ng-app="app" and ng-controller="Student" is already injected 

<script src="app/js/jquery/jquery-3.1.1.min.js"></script> 
<script src="app/js/angularjs/angular.min.js"></script> 
<script src="app/controller/app.js"></script> 
<script src="app/service/student-service.js"></script> 
<script src="app/controller/student-controller.js"></script> 
<script src="app/css/bootstrap/js/bootstrap.min.js"></script> 

app.js:

(function() { 

'use strict'; 
angular.module('app', [ 
    'Student', 
    'StudentService' 
]); 

})(); 

學生controller.js:

angular.module('Student', ['StudentService']) 
    .controller('Student', ['$scope', '$http', 'StudentService', 
     function($scope, $http, $studentService) { 

     // a function here which calls studentService 

     }]); 

學生service.js:

angular.module('StudentService', []) 
    .factory('StudentService', ['$http', '$q', 
     function($http, $q) { 

      return { 
       getStudentData : getStudentData 
      } 

      // getstundetData function here 


}]); 

當我在我的控制器中的函數中調用studentService時出現錯誤,說studentService是未定義的!我不知道什麼是真的錯,但我認爲我的依賴的流程是正確的。 。

首頁 - > app.js->控制器 - >服務

你能幫助我的傢伙?謝謝。 。 。

回答

1

改變順序,因爲app.js是其它兩個模塊depdendent,

<script src="app/service/student-service.js"></script> 
<script src="app/controller/student-controller.js"></script> 
<script src="app/controller/app.js"></script> 
+0

沒有進展先生sajee :( –

+0

.controller( '學生',['$ scope','$ http','StudentService', 函數($ scope,$ http,studentService){ – Sajeetharan

+0

Omg!謝謝先生sajee我的眼睛在背叛我:( –

2

變化$studentService控制器注入到StudentService

.controller('Student', ['$scope', '$http', 'StudentService', 
     function($scope, $http,StudentService) { 

     // a function here which calls studentService 

     }]); 
+0

謝謝先生! :) –