如何检测用户是否单击了Angularjs中的浏览器后退按钮 [英] How to detect if a user clicks browser back button in Angularjs

查看:131
本文介绍了如何检测用户是否单击了Angularjs中的浏览器后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些数据的类似表单的页面.并希望在用户单击浏览器后退按钮时显示弹出窗口/警告,询问他们是否要返回或停留在同一页面上".我正在使用angular-ui-router的$stateProvider,并希望将其仅绑定到一个状态/视图.

I have a form-like page with some data. And want to show a popup/alert when a user clicks the browser back button, asking "if they want to go back or stay on the same page". I am using angular-ui-router's $stateProvider and want to bind this only to one state/view.

推荐答案

这是我先前对其他问题的回答,但对您有所帮助

您可以使用$routeChangeStart

在更改路线之前广播.此时,路由服务开始解决发生路由更改所需的所有依赖关系.通常,这涉及获取视图模板以及在resolve route属性中定义的所有依赖项.解决所有依赖性后,将触发$ routeChangeSuccess.

Broadcasted before a route change. At this point the route services start resolving all of the dependencies needed for the route change to occur. Typically this involves fetching the view template as well as any dependencies defined in resolve route property. Once all of the dependencies are resolved $routeChangeSuccess is fired.

可以通过调用事件的preventDefault方法来防止路由更改(以及触发它的$location更改).有关事件对象的更多详细信息,请参见$rootScope.Scope.

The route change (and the $location change that triggered it) can be prevented by calling preventDefault method of the event. See $rootScope.Scope for more details about event object.

因此,请尝试以下代码.

So please try this below code.

  $scope.$on('$routeChangeStart', function (scope, next, current) {
        if (next.$$route.controller != "Your Controller Name") {
            // Show here for your model, and do what you need**
            $("#yourModel").show();
        }
    });

更新:

您需要在模型弹出窗口中编写功能性工作.像

Update:

You need to write your functional work in the model popup. like

放置一些链接按钮

  • 确定要转到上一页吗?
  • 您要保留当前页面吗?
  • 您要注销吗?等等

然后将ng-click事件添加到上一页,使用return false保持当前页面,等等.

then Add ng-click event for go prev page, stay current page with using return false, etc.

是否做出了花花公子?

这篇关于如何检测用户是否单击了Angularjs中的浏览器后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆