【问题标题】:Cookies with routing angularjs带有路由 angularjs 的 Cookie
【发布时间】:2017-06-26 20:29:19
【问题描述】:

我正在使用 angularjs 登录我的网站。我的代码:
HTML:

        <input type="text" ng-model="username" placeholder="Username">

        <input type="password" ng-model="password" placeholder="Password" >

        <button class="btn btn-lg btn-primary btn-block" type="submit" ng-click="signin()">Sign in</button> 

JS:

app.config(function($routeProvider) {
        $routeProvider
            .when('/', {
                 resolve: {
                    "check": function($location, $rootScope) {
                        if(!$rootScope.valueCookie) {
                            $location.path('/login');
                        }
                    }
                },
                templateUrl: 'home.html'
            })
            .when('/login', {
                templateUrl: 'login.html'
            })
            .otherwise({
                redirectTo: '/'
            });
    });

 $scope.signin = function() {
        $rootScope.valueCookie = $cookieStore.put('obj', someSessionObj);
    }

我尝试做的是 cookie 解析。当cookies在浏览器中时——点击登录后,我们在“home.html”中,刷新页面后仍然有“home.html”。我的代码不起作用。 感谢您提前回答。

【问题讨论】:

    标签: javascript angularjs cookies


    【解决方案1】:

    你刚刚为 $rootScope.valueCookie 设置了cookie,你应该得到它并检查它是否存在。将您的代码更改为

     $cookies.put('obj', someSessionObj); 
     $rootScope.valueCookie = $cookies.get('obj');
    

    来自文档:不推荐使用 $cookieStore:(自 v1.4.0 起)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 2015-05-30
      相关资源
      最近更新 更多