如何检测用户是否单击Angularjs中的浏览器后退按钮单击、按钮、浏览器、用户

2023-09-06 08:25:11 作者:对不起づ我爱你

我有一个类似表单的页面,其中包含一些数据.并希望在用户单击浏览器后退按钮时显示弹出/警报,询问他们是要返回还是留在同一页面上".我正在使用 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.

推荐答案

这是我之前对其他问题的回答,不过应该能帮到你

您可以使用 angular $routeChangeStart

You can do it by using angular $routeChangeStart

在路线更改之前广播.此时,路由服务开始解决发生路由更改所需的所有依赖项.通常这涉及获取视图模板以及在解析路由属性中定义的任何依赖项.一旦解决了所有依赖项,就会触发 $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

添加一些链接按钮

确定要转到上一页吗?您想保持当前页面吗?您要退出吗?等

然后为 go prev 页面添加 ng-click 事件,使用 return false 等保持当前页面.

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

 
精彩推荐
图片推荐