Я нашел этот красивый метод, чтобы поймать window.onbeforeunload
событие в угловом. Теперь я хочу открыть свое модальное окно, но я не знаю, как это сделать.
Это метод catch:
examinationApp.factory('beforeUnload', function($rootScope, $window) {
// Events are broadcast outside the Scope Lifecycle
$window.onbeforeunload = function(e) {
var confirmation = {};
var event = $rootScope.$broadcast('onBeforeUnload');
if (event.defaultPrevented) {
return confirmation.message;
}
};
$window.onunload = function() {
$rootScope.$broadcast('onUnload');
};
return {};
})
.run(function(beforeUnload) {
// Must invoke the service at least once
});
examinationApp.controller("examinationCtrl", ['$scope', '$window', '$http', '$cookieStore', function ($scope, $window, $http, modalDialog) {
$scope.$on('onBeforeUnload', function (e, confirmation) {
confirmation.message = confirmation.message = "All data willl be lost.";
e.preventDefault();
});
$scope.$on('onUnload', function (e) {
console.log('leaving page'); // Use 'Preserve Log' option in Console
});
В моем контроллере у меня есть переменная "modalEndShow=false"
. Я хочу переключить его, когда будет наблюдаться событие.
examinationApp.controller("examinationCtrl", ['$scope', '$window', '$http', '$cookieStore', function ($scope, $window, $http, modalDialog) {
modalEndShown=false;}]);
Итак, как я могу это сделать? Спасибо за внимание.
Вы можете сделать этот модальный поп, сделав это:
$scope.$on('onBeforeUnload', function (e, confirmation) {
confirmation.message = confirmation.message = "All data willl be lost.";
e.preventDefault();
$scope.$apply(function() {
$scope.modalEndShown = true;
});
});