2016-12-22 5 views
0

Ich möchte nur fragen, was ist falsch mit meinen Codes hier?Angularjs: Service ist undefined

Index:

// 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' 
]); 

})(); 

Schüler-controller.js:

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

     // a function here which calls studentService 

     }]); 

Schüler-service.js:

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

      return { 
       getStudentData : getStudentData 
      } 

      // getstundetData function here 


}]); 

Wenn ich studentService in einer Funktion in meinem Controller anrufe, habe ich einen Fehler erhalten, der besagt, dass studentService nicht definiert ist! Ich weiß nicht, was wirklich falsch ist, aber ich denke, der Fluss meiner Abhängigkeit ist richtig. .

index-> ​​app.js-> Controller-> Service

Können Sie mir helfen Jungs? Vielen Dank. . .

Antwort

1

Ändern der Reihenfolge, weil app.js depdendent auf beiden anderen Modulen ist,

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

kein Fortschritt sir sajee :( –

+0

.Controller ('Student', '$ scope', '$ http', 'StudentService', Funktion ($ scope, $ http, studentService) { – Sajeetharan

+0

Omg! Danke, Sir Sajee mein Auge betrügt mich :( –

2

Änderung der Controller inject von $studentService zu StudentService

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

     // a function here which calls studentService 

     }]); 
+0

Danke, mein Herr! :) –