Angularjs:服务未定义

时间:2016-12-22 02:57:37

标签: 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'
]);

})();

student-controller.js:

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

        // a function here which calls studentService

        }]);

student-service.js:

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

            return {
                getStudentData :  getStudentData
            }

            // getstundetData function here


}]);

当我在我的控制器中的函数中调用studentService时,我得到错误,说studentService未定义!我不知道什么是错的,但我认为我的依赖是正确的。

索引 - &GT; app.js-&GT;控制器 - &GT;服务

你能帮帮我们吗?谢谢。 。

2 个答案:

答案 0 :(得分:3)

$studentService的控制器注入更改为StudentService

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

        // a function here which calls studentService

        }]);

答案 1 :(得分:2)

更改顺序,因为app.js在其他两个模块上是depandent,

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