从一个页面重定向到另一页面时,我需要共享一些数据。我通过有角度的服务来做到这一点。
var ProductionControl = angular.module('ProductionControl', ['ngRoute', 'ngGrid', 'ngAnimate', 'angularFileUpload', 'ui.bootstrap', 'toaster']);
// configure our routes
ProductionControl.value("Company", { Name: "" });
ProductionControl.value("FromPage", { Name: "" });
ProductionControl.value("loggedUserName", { Name: "" });
ProductionControl.value("Version", { Name: "" });
ProductionControl.constant('RoutingStatus', {
New: 1,
Certified: 2,
UnderDevelopment: 3,
Initial: 4
});
ProductionControl.service('PmsDataSharingService', function () {
var sharedData;
this.setSharedData = function (d) {
sharedData = d;
}
this.getSharedData = function () {
return sharedData;
}
});
我这样设置数据 在控制器中:
var objectSent = {
BatchNo: $scope.BatchNo,
RoutingStageId: $scope.RoutingStageId,
LineId: $scope.LineId,
PartNumber: $scope.PartNumber
}
PmsDataSharingService.setSharedData(objectSent);
window.location.href = '#EquipmentSetup';
我得到这样的数据 在另一个控制器中
var objectSent = PmsDataSharingService.getSharedData();
if (!$.isEmptyObject(objectSent)) {
///
/// do something
///
现在,我需要在退出此页面时清除此共享数据。我怎样才能做到这一点? 我怎么知道用户将要退出页面?有什么办法吗? 我尝试了这个但是没用
在app.js中
ProductionControl.config(function ($locationProvider, $routeProvider) {
/// routing configs
})
.run(function ($rootScope, $location, Company) {
// register listener to watch route changes
$rootScope.$on("$routeChangeStart", function (event, next, current) {
if (next.templateUrl != "Partials/EquipmentSetup.html") {
//Redirection to login not required
var objectSent = {};
PmsDataSharingService.setSharedData(objectSent);
}
});
获取控制台错误:未定义PmsDataSharingService。
编辑:
当我将$ on函数放在使用共享值的控制器中时,它起作用了
$rootScope.$on("$routeChangeStart", function (event, next, current) {
if (next.templateUrl != "Partials/EquipmentSetup.html") {
//Redirection to login not required
var objectSent = {};
PmsDataSharingService.setSharedData(objectSent);
}
});