【发布时间】:2014-04-14 08:03:30
【问题描述】:
我的app.js 中有三个路由,但是当我从创建切换到任何其他路由时,我想检查用户是否有意点击切换或者是错误,所以会有返回弹出窗口询问用户留在页面上或离开页面。在通常情况下,它可以使用以下代码:
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. Are you sure?";
}
但由于这是一个 SPA 应用程序,我必须在切换路线时运行此功能:
这是我的路线
testApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/tests', {
templateUrl: 'partials/list.html',
controller: 'ListCtrl'
}).
when('/tests/:testsId', {
templateUrl: 'partials/detail.html',
controller: 'DetailCtrl'
}).
when('/create/', {
templateUrl: 'partials/create.html',
controller: 'CreateCtrl'
}).
otherwise({
redirectTo: '/tests'
});
}]);
非常感谢任何帮助。
【问题讨论】:
标签: angularjs routes onbeforeunload