I am a new bee. I am trying to write a confirmation that warns the user when he tries to close the browser window.
I have 2 links on my page v1 and v2. When I click on the links that it takes to certain 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 display a message when the user clicks on v1 so that "he was going to leave v1 if he wants to continue" and when I click on v2. Any pointers on how to achieve this would be appreciated.
I got a response here , but it gives a 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 = [];
javascript angularjs model-view-controller angularjs-directive
iJade Feb 11 '13 at 9:55 on 2013-02-11 09:55
source share