Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing alert in angularjs when user leaves a page

I'm an angularjs new bee. I'm trying to write a validation which alerts the user when he tries to close the browser window.

I have 2 links on my page v1 and v2.When clicked on the links it takes to the specific pages. Here is the code to redirect to v1 and 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'}); }]); 

I want to pop up a message when the user clicks on v1 that "he's about to leave from v1, if he wishes to continue" and same on clicking on v2. Any pointers on how to achieve this would be appreciated.

I got an answer here but it pops up the message after every time interval.

Updated Code;

Controllers

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 = []; 
like image 414
iJade Avatar asked Feb 11 '13 09:02

iJade


People also ask

What is $Watch in AngularJS?

$watch() function is used to watch the changes of variables in $scope object. Generally the $watch() function will create internally in Angularjs to handle variable changes in application. Let us understand with the following example to understand how to implement $watch().

What is $emit in AngularJS?

$emit() Function. The AngularJS $emit method is used to dispatches an event name upwards and travel up to $rootScope. scope listeners. The $emit propagatesevent name upward and travel upwards toward $rootScope. scope listeners and calls all registered listeners along the way.

What happens when page containing AngularJS based application?

Q 6 - What happens when page containing AngularJS based Application loads. A - HTML document is loaded into the browser, and evaluated by the browser. B - AngularJS JavaScript file is loaded; the angular global object is created. C - JavaScript which registers controller functions is executed.


2 Answers

The code for the confirmation dialogue can be written shorter this way:

$scope.$on('$locationChangeStart', function( event ) {     var answer = confirm("Are you sure you want to leave this page?")     if (!answer) {         event.preventDefault();     } }); 
like image 147
Scheintod Avatar answered Sep 19 '22 08:09

Scheintod


Lets seperate your question, you are asking about two different things:

1.

I'm trying to write a validation which alerts the user when he tries to close the browser window.

2.

I want to pop up a message when the user clicks on v1 that "he's about to leave from v1, if he wishes to continue" and same on clicking on v2.

For the first question, do it this way:

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; } 

And for the second question, do it this way:

You should handle the $locationChangeStart event in order to hook up to view transition event, so use this code to handle the transition validation in your controller/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();         }     }); } 
like image 25
Yair Nevet Avatar answered Sep 18 '22 08:09

Yair Nevet