错误:AngularJS中的[$ injector:unpr]

时间:2018-07-22 14:16:32

标签: angularjs angularjs-service angularjs-controller

我正在研究一个webApi项目。我想在数据库中列出学生。当我不使用服务(工厂)来执行此操作时,它将起作用。但是,当我创建服务时,它无法正常工作,并且出现错误。我知道这个网站也有同样的问题。但是答案没有用,所以我问。这是我的文件;

studentServis.js:

(function () {
'use strict';

angular
    .module('studentApp')
    .factory('studentService', [
        "$http","studentApiUrl",
        function ( $http,studentApiUrl) {
            var getAll = function () {
                return $http.get(studentApiUrl);
            };   
            var service = {
                getAll: getAll
            };

            return service;
        }

    ]);

})();

studentController.js:

(function () {
'use strict';

angular
    .module('studentApp')
    .controller('studentController', [
        "$scope", "studentService",
        function ($scope, studentService) {
            $scope.message = "hey";

            studentService.getAll().then(function (data) {
                $scope.students = data;
            });
        }

    ]); })();

studentApp.js:

(function () {
'use strict';

var app=angular.module('studentApp', [
    // Angular modules 
    'ngRoute'
]).constant('studentApiUrl', "/api/student/"); })();

Index.html:

 <div ng-app="studentApp">
    <div ng-controller="studentController">
        {{message}}

        <table class="table table-bordered">
            <tbody>
                <tr ng-repeat="student in students">
                    <td>{{student.id}}</td>
                    <td>{{student.firstname}}</td>
                    <td>{{student.lastname}}</td>
                    <td>{{student.university}}</td>
                    <td>
                        <a href="#/details/{{student.id}}" class="btn btn-default btn-sm">Edit</a>
                        <button class="btn btn-danger btn-sm" ng-click="delete(student)">Delete</button> -->
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

$scope.message(嘿)部分仅供尝试。它也不会打印。输出在网站中:

{{message}}

与我的学生名单无关。

这是错误:

  

http://errors.angularjs.org/1.7.2/$injector/unpr?p0=studentServiceProvider%20%3C-%20studentService%20%3C-%20studentController

angular.js:15018 Error: [$injector:unpr] 
at angular.js:99
at angular.js:4891
at Object.d [as get] (angular.js:5051)
at angular.js:4896
at d (angular.js:5051)
at e (angular.js:5076)
at Object.invoke (angular.js:5100)
at P.instance (angular.js:11160)
at p (angular.js:10031)
at g (angular.js:9370)

我看过这里,但我认为我正确地告诉了这里:AngularJS Error Reference - $injector unpr

这是索引中的引用:

<head>
<meta charset="utf-8" />
<title></title>
<script src="../../Scripts/angular.min.js"></script>
<script src="../../Scripts/angular.js"></script>
<script src="../../Scripts/angular-route.js"></script>
<script src="../Scripts/studentApp.js"></script>
<script src="../Scripts/studentServis.js"></script>
<script src="../Scripts/studentController.js"></script>

<link href="../../Content/Site.css" rel="stylesheet" />
<link href="../../Content/bootstrap.css" rel="stylesheet" />  

好的,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

在引用中加载服务的地方看不到,顺序应该是

<script src="../Scripts/studentApp.js"></script>
<script src="../Scripts/studentServis.js"></script>
<script src="../Scripts/studentController.js"></script>