使用AngularJS的未知提供程序错误

时间:2017-04-26 15:46:14

标签: javascript angularjs model-view-controller

我是使用AngularJS的新手,并出现以下错误:

angular.js:14525错误:[$ injector:unpr]未知提供者:reservationsFactoryProvider< - reservationsFactory< - MainPageController< - RecordsController

我的主要jsp页面是

`<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<!doctype html>
<html data-ng-app="serverApp">
<head>
    <script src="./resources/js/libs/angular.js"></script>
    <script src="./resources/js/libs/angular-route.js"></script>
    <script type="text/javascript" src="./resources/js/libs/angular-resource.js"></script>
    <link rel="stylesheet" type="text/css" href="./resources/css/style.css" >
</head>
<body>

<!-- header begin -->
<div id="top"></div>
<div id="link_bar">
    &nbsp;&nbsp;&nbsp;<a href="#!/main_page">Main</a>&nbsp;|&nbsp;Products&nbsp;|&nbsp;Contact us
</div>
<div id="login_bar">
</div>
<div id="submenu"> Reserved Area:&nbsp;&nbsp;&nbsp;<a href="#!/reserved_area">View1</a>&nbsp;|&nbsp;View2</div> 
<div id="partials">
    <div data-ng-view></div>
</div>
<div id="bottom"></div>
<script src="./resources/js/app.js"></script>
</body>
</html>`

和.js文件:

    var serverApp = angular.module('serverApp', [ 'ngRoute' , 'ngResource' ]);

serverApp.config(['$routeProvider', function($routeProvider){
    $routeProvider.when('/main_page', {
        templateUrl: './resources/partials/part_main.jsp',
        controller: 'MainPageController'
    });
    $routeProvider.when('/reserved_area', {
        templateUrl: './resources/partials/part_reserv.jsp',
        controller: 'RecordsController'
    });
    $routeProvider.otherwise({redirectTo: '/main_page'});
}]);


serverApp.controller('MainPageController', ['$scope', '$rootScope', '$routeParams', 'reservationsFactory',
    function ($scope, $rootscope, $routeParams, reservationsFactory) {

    }
]);

serverApp.controller('RecordsController', ['$scope', '$rootScope', '$routeParams', '$location', 'reservationsFactory','slotsFactory',
    function ($scope, $rootScope, $routeParams, $location, recordsFactory) {
    $scope.test="BLABLA";
    console.log("Controller works");
    var promRecords=recordsFactory.query();//not ready
     var records=[]

     $scope.records=records;

     promRecords.$promise.then(
             function(result){
                 for (var i = 0; i < result.length; i++) {
                     reservations[i]=result[i];
                 }             
                 console.log("DATA ARRIVED!");
             } ,
             function(error) {
                 alert("You must be logged in!");
             }
     );

    }]
);

serverApp.factory('recordsFactory',['$resource',function ($resource){
    return $resource("http://localhost:8080/My_Server/records",null,{ 'update': {method: 'PUT'} });
}]);

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您已注册recordsFactoryserverApp.factory('recordsFactory',...),您还必须注册reservationsFactory,而slotsFactory与Angular工厂的注册方式相同。