在角JS应用程序的后退按钮行为 [英] back button behavior in angular js application

查看:128
本文介绍了在角JS应用程序的后退按钮行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有写在一个angularJS SPA的应用程序的问题。该应用程序包括THRE的观点 - 登录视图,主视图和注销视图。

I have an issue with a SPA app written in angularJS. The application consist of thre views - login view, main view and log out view.


  • 的myapp /#/登录

  • 的myapp /#/主

  • 的myapp /#/注销

我的路线提供商:

function Router($routeProvider) {
    $routeProvider
        .when('/login', {
            templateUrl: 'app/components/login/login.tmpl.html',
            controller: 'login.ctrl',
            controllerAs: 'vm'
        })
        .when('/main', {
            templateUrl: 'app/components/dashboard/main.tmpl.html',
            controller: 'dashboard.ctrl',
            controllerAs: 'vm'
        })
        .when('/logout', {
            templateUrl: 'app/components/logout/logout.tmpl.html',
            controller: 'logout.ctrl',
            controllerAs: 'vm'
        })
        .otherwise({
            redirectTo: '/login'
        });
}

使用情况:
   1)用户登录。
   2)用户将看到在主视图内容
   3)用户注销。
   4)用户重定向到确认注销视图
   5)用户被重定向到登录视图

Use case: 1) user logs in. 2) user sees the content in the main view 3) user logs out. 4) user is redirected to the logout view for confirmation 5) user is redirected to the login view

如果我preSS后浏览器按钮,第5步之后,我重定向到主视图。有没有办法来改变后退按钮的行为,当我在登录看法?我相信这是一些简单的,我道歉,如果这个问题是重复的。感谢您的时间和答复!

If I press the back browser button after step 5 I am redirected to the main view. Is there a way to change the behavior of the back button when I am in the login view? I believe it is something simple and I apologize if the question is duplicated. Thank you for your time and responses!

推荐答案

请参见工作示例:的 http://plnkr.co/edit/46O0znC5HFDE4cYXSm5h?p=$p$pview

在cookies在登录功能存储的数据如下,

Stored data in cookies in login function as follows,

$cookies.userinfo = {
    'id': 1,
    'name': 'pqr'
};

和在注销删除数据 -

And on logout remove that data -

delete $cookies.userinfo;

然后检查'angular.isDefined($ cookies.userinfo)(用户信息是存储在它的数据的时间定的cookie名称),如果找到则重定向到您的网页,其中你想登录后看到的。即

then check for 'angular.isDefined($cookies.userinfo)' (userinfo is cookie name which given at the time of storing data in it) if find then redirect it to your page which you want to see after login. i.e

app.run(function ($cookies) {
      $rootScope.$on("$routeChangeStart", function () {
        if (angular.isDefined($cookies.userinfo)) {
            $location.path("/pathname");
        }

      });
});

这篇关于在角JS应用程序的后退按钮行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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