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 = [];
$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().
$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.
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.
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(); } });
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(); } }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With