我是一个新的角色js。我试图编写一个验证,当他试图关闭浏览器窗口时提醒用户。当用户离开页面时显示在angularjs中的警报
我在我的页面v1和v2上有2个链接。当点击链接到特定页面时。 下面是代码重定向到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上。 任何关于如何实现这一点的指针,将不胜感激。
我得到了一个答案here,但它在每个时间间隔后弹出消息。
更新后的代码;
控制器
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 = [];
我相信你想要的术语是 “[新手](http://www.urbandictionary.com/define.php?term=newbie)”。除非你真的是一只意大利蜜蜂。 – 2015-12-15 16:22:27