我是一个angularjs新蜂。 我试着写,当他试图关闭浏览器窗口,提醒用户验证。
我有我的网页V1 2个链接和v2.When点击它采取的特定页面的链接。 这里是重定向到v1和v2代码
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/v1', {templateUrl: 'pages/v_1.html', controller: MyCtrl1});
$routeProvider.when('/v2', {templateUrl: 'pages/v_2.html', controller: MyCtrl2});
$routeProvider.otherwise({redirectTo: '/v1'});
}]);
我想,当用户点击V1说,“他是即将从V1离开,如果他希望继续”,并在相同点击V2弹出一个消息。 关于如何实现这一目标的任何指针将不胜感激。
我得到了一个答案在这里 ,但它的每一个时间间隔后弹出的消息。
更新后的代码;
控制器
function MyCtrl1() {
$scope.$on('$locationChangeStart', function (event, next, current) {
if ('your condition') {
event.preventDefault();
MessageService.showConfirmation(
'Are you sure?',
MessageService.MessageOptions.YES_NO, {
'YES': function () {
blockNavigation = false;
$location.url($location.url(next).hash());
$rootScope.$apply();
},
'NO': function () {
MessageService.clear();
$log.log('NO Selected')
}
});
}
});
}
MyCtrl1.$inject = [];
function MyCtrl2() {}
MyCtrl2.$inject = [];
在确认对话框中的代码可以写成短是这样的:
$scope.$on('$locationChangeStart', function( event ) {
var answer = confirm("Are you sure you want to leave this page?")
if (!answer) {
event.preventDefault();
}
});
让我们单独你的问题,你问的是两种不同的东西:
1。
我试着写,当他试图关闭浏览器窗口,提醒用户验证。
2。
我想,当用户点击V1说,“他是即将从V1离开,如果他希望继续”,并在相同点击V2弹出一个消息。
对于第一个问题,做这种方式:
window.onbeforeunload = function (event) {
var message = 'Sure you want to leave?';
if (typeof event == 'undefined') {
event = window.event;
}
if (event) {
event.returnValue = message;
}
return message;
}
而对于第二个问题,做这种方式:
你应该处理$locationChangeStart
以挂钩查看转换事件时,使用此代码来处理过渡验证在你的控制器/ S:
function MyCtrl1($scope) {
$scope.$on('$locationChangeStart', function(event) {
var answer = confirm("Are you sure you want to leave this page?")
if (!answer) {
event.preventDefault();
}
});
}
下面是我用的指令。 当窗体卸载它会自动清理自己的身体。 如果你想防止提示从发射(例如,因为你成功保存的形式),调用$ scope.FORMNAME。$ setPristine(),在窗体名称是要从提示,防止表单的名称。
.directive('dirtyTracking', [function () {
return {
restrict: 'A',
link: function ($scope, $element, $attrs) {
function isDirty() {
var formObj = $scope[$element.attr('name')];
return formObj && formObj.$pristine === false;
}
function areYouSurePrompt() {
if (isDirty()) {
return 'You have unsaved changes. Are you sure you want to leave this page?';
}
}
window.addEventListener('beforeunload', areYouSurePrompt);
$element.bind("$destroy", function () {
window.removeEventListener('beforeunload', areYouSurePrompt);
});
$scope.$on('$locationChangeStart', function (event) {
var prompt = areYouSurePrompt();
if (!event.defaultPrevented && prompt && !confirm(prompt)) {
event.preventDefault();
}
});
}
};
}]);
正如你在前面已经发现,可以使用的组合window.onbeforeunload
和$locationChangeStart
到消息的用户。 此外,你可以利用ngForm.$dirty
只有消息的用户,当他们做出改变。
我已经写了,你可以应用到任何形式的,如果他们重新加载页面或导航离开,将自动更改消息的用户观看的angularjs指令。 @see https://github.com/facultymatt/angular-unsavedChanges
希望你找到这个指令有用!
这里的其他例子做工精细的老版本用户界面路由器(> = 0.3.X),但所有状态的事件,如$stateChangeStart
,被弃用的1.0 。 新的UI路由器1.0代码使用$过渡服务 。 所以,你需要注入$transitions
到组件然后使用$ transitions.onBefore方法,下面的代码演示。
$transitions.onBefore({}, function(transition) {
return confirm("Are you sure you want to leave this page?");
});
这仅仅是一个超级简单的例子。 在$transitions
服务可以接受如许诺更复杂的反应。 见HookResult类型以获取更多信息。
$scope.rtGo = function(){
$window.sessionStorage.removeItem('message');
$window.sessionStorage.removeItem('status');
}
$scope.init = function () {
$window.sessionStorage.removeItem('message');
$window.sessionStorage.removeItem('status');
};
刷新页面:在使用init