Javascript TypeError:reservationService.GetPendingrervations不是函数
angular.js:13708类型错误:reservationService.GetPendingrervations 这不是一个函数 当我第一次加载页面时,我得到了这个错误,但给F5页面,加载平稳 下面我展示我的代码。这是我找不到的错误。我已经检查了一些论坛,这些论坛表明它可能与依赖注入有关,但仍然找不到错误。我做了一个测试,我意识到当页面第一次加载时,工厂是未初始化的,如果这与此无关的话 homeController.jsJavascript TypeError:reservationService.GetPendingrervations不是函数,javascript,html,angularjs,Javascript,Html,Angularjs,angular.js:13708类型错误:reservationService.GetPendingrervations 这不是一个函数 当我第一次加载页面时,我得到了这个错误,但给F5页面,加载平稳 下面我展示我的代码。这是我找不到的错误。我已经检查了一些论坛,这些论坛表明它可能与依赖注入有关,但仍然找不到错误。我做了一个测试,我意识到当页面第一次加载时,工厂是未初始化的,如果这与此无关的话 homeController.js 'use strict'; app.controller('hom
'use strict';
app.controller('homeController', ['$scope', '$route', '$location', 'authService', 'reservationService', 'localStorageService',
function ($scope, $route, $location, authService, reservationService, localStorageService) {
$scope.showNotifications = function () {
updateNotifications();
$("#notificationContainer").fadeToggle(600);
//hideCountNotifications();
}
function updateNotifications() {
reservationService.getPendingReservations(authData.id_place).then(function (results) {
if (results.status != 200) {
$scope.message = "Upps! Lo siento, creo que tenemos un problema al cargar las notificaciones.";
console.log(results);
}
else {
$scope.notifications = results.data;
$scope.cantPendingReservations = results.data.length;
//// La primera vez que carga la pagina se llamada el metodo para que recargue la grilla
//reloadGrid();
}
//$scope.$emit('UNLOAD')
//$("#tableScheduler").show();
}, function (error) {
if (error.status == 401) { authService.logOut(); $location.path('/login'); }
});
}
}]);
'use strict';
app.factory('reservationService', ['$http', '$q', 'localStorageService', 'servicesConnect', function ($http, $q, localStorageService, servicesConnect) {
var serviceBase = servicesConnect.apiFutbolPlay;
var reservationService = {};
var authData = localStorageService.get('authorizationData');
if (authData) {
var _ListPitch = function (id_place) {
var deferred = $q.defer();
$http.get(serviceBase + 'api/pitches/single/' + id_place).then(function (results) {
deferred.resolve(results);
}, function errorCallback(err, status) {
deferred.reject(err);
});
return deferred.promise;
}
var _ListPendingReservations = function (id_place) {
var deferred = $q.defer();
$http.get(serviceBase + 'api/reservations/customer/myreservations/' + id_place, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then(function (results) {
deferred.resolve(results);
}, function errorCallback(err, status) {
deferred.reject(err);
});
return deferred.promise;
}
reservationService.getPendingReservations = _ListPendingReservations;
}
return reservationService;
}]);
reservationService.js
'use strict';
app.controller('homeController', ['$scope', '$route', '$location', 'authService', 'reservationService', 'localStorageService',
function ($scope, $route, $location, authService, reservationService, localStorageService) {
$scope.showNotifications = function () {
updateNotifications();
$("#notificationContainer").fadeToggle(600);
//hideCountNotifications();
}
function updateNotifications() {
reservationService.getPendingReservations(authData.id_place).then(function (results) {
if (results.status != 200) {
$scope.message = "Upps! Lo siento, creo que tenemos un problema al cargar las notificaciones.";
console.log(results);
}
else {
$scope.notifications = results.data;
$scope.cantPendingReservations = results.data.length;
//// La primera vez que carga la pagina se llamada el metodo para que recargue la grilla
//reloadGrid();
}
//$scope.$emit('UNLOAD')
//$("#tableScheduler").show();
}, function (error) {
if (error.status == 401) { authService.logOut(); $location.path('/login'); }
});
}
}]);
'use strict';
app.factory('reservationService', ['$http', '$q', 'localStorageService', 'servicesConnect', function ($http, $q, localStorageService, servicesConnect) {
var serviceBase = servicesConnect.apiFutbolPlay;
var reservationService = {};
var authData = localStorageService.get('authorizationData');
if (authData) {
var _ListPitch = function (id_place) {
var deferred = $q.defer();
$http.get(serviceBase + 'api/pitches/single/' + id_place).then(function (results) {
deferred.resolve(results);
}, function errorCallback(err, status) {
deferred.reject(err);
});
return deferred.promise;
}
var _ListPendingReservations = function (id_place) {
var deferred = $q.defer();
$http.get(serviceBase + 'api/reservations/customer/myreservations/' + id_place, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then(function (results) {
deferred.resolve(results);
}, function errorCallback(err, status) {
deferred.reject(err);
});
return deferred.promise;
}
reservationService.getPendingReservations = _ListPendingReservations;
}
return reservationService;
}]);
index.html
<!DOCTYPE html>
<html data-ng-app="FutPlayApp">
<head>
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="images/favicon/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
</head>
<body>
<div class="container" data-ng-controller="homeController">
<div data-ng-view="">
</div>
<div class="clearfix"></div>
</div>
<!-- Jquery -->
<script src="scripts/jquery-2.2.4.min.js"></script>
<!-- Bootstrap -->
<script src="scripts/bootstrap.min.js"></script>
<!-- Load Angular -->
<script src="scripts/angular.min.js"></script>
<script src="scripts/angular-route.min.js"></script>
<script src="scripts/loading-bar.min.js"></script>
<script src="scripts/angular-local-storage.min.js"></script>
<script src="scripts/moment.js"></script>
<script src="scripts/moment-timezone-with-data.js"></script>
<script src="scripts/datetimepicker.js"></script>
<script src="scripts/datetimepicker.templates.js"></script>
<script src="scripts/dateTimeInput.js"></script>
<!--Autocompletado-->
<script src="scripts/angucomplete-alt.js"></script>
<!-- Load app main script -->
<script src="app/app.js"></script>
<!-- Notificaciones de reservas de usuario -->
<script type="text/javascript" src="scripts/pnotify.custom.min.js"></script>
<!-- Load services -->
<script src="app/services/authInterceptorService.js"></script>
<script src="app/services/authService.js"></script>
<script src="app/services/reservationService.js"></script>
<script src="app/services/schedulerService.js"></script>
<script src="app/services/profileService.js"></script>
<script src="app/services/changepasswordService.js"></script>
<script src="app/services/reservationmultipleService.js"></script>
<!-- Load controllers -->
<script src="app/controllers/loginController.js"></script>
<script src="app/controllers/reservationController.js"></script>
<script src="app/controllers/homeController.js"></script>
<script src="app/controllers/schedulerController.js"></script>
<script src="app/controllers/profileController.js"></script>
<script src="app/controllers/changepasswordController.js"></script>
<script src="app/controllers/reservationmultipleController.js"></script>
</body>
</html>
错误的原因是
reservationService。只有当本地存储中存在'authorizationData'
时,才会初始化GetPendingrervations
。如果没有,则getPendingrervations
永远不会设置为任何值
我的猜测是,在代码的其他地方,authorizationData
存储在本地存储器中,因此当您刷新页面时,该函数将可用
要解决此问题,您需要重组您的预订服务,以应对未经授权的情况,或者在尝试使用
GetPendingrervations
之前进行检查。我想感谢您的回答,但我没有至少15分的分数。但我运行你的解决方案。哥伦比亚的问候。