简体   繁体   English

重新启动浏览器后,angular ngCookies不会保存cookie

[英]angular ngCookies doesn't save cookies after restarting the browser

  var app = angular.module('MyApp', ['ngCookies']); app.controller('MyController', function ($scope, $window, $cookies) { $scope.WriteCookie = function () { $cookies.put("Name", $scope.Name); }; $scope.ReadCookie = function () { $window.alert($cookies.get('Name')); }; $scope.RemoveCookie = function () { $cookies.remove('Name'); }; }); 
 <html> <head> <title></title> </head> <body> <div ng-app="MyApp" ng-controller="MyController"> Name: <input type="text" ng-model="Name" /> <br /> <br /> <input type="button" value="Write Cookie" ng-click="WriteCookie()" /> <input type="button" value="Read Cookie" ng-click="ReadCookie()" /> <input type="button" value="Remove Cookie" ng-click="RemoveCookie()" /> </div> </body> </html> 

I wrote this simple demp app for cookies in angular. 我用角度写了这个简单的demp app for cookies。

whenever I enter this (via nodeJS) and go to this url: http://127.0.0.1:8080 每当我输入(通过nodeJS)并转到此URL: http//127.0.0.18080

the cookies is presisted in the browser's cookies. cookie存储在浏览器的cookie中。 when I click refresh or close the tab and opening a new tab with this url (without closing the browser) the cookies stays. 当我单击刷新或关闭选项卡并使用此URL打开新选项卡(不关闭浏览器)时,cookie保持不变。

However, when I close the browser (chrome/IE/firefox) and open this url again, the cookies is not there! 但是,当我关闭浏览器(chrome / IE / firefox)并再次打开此URL时,cookie就不存在!

why? 为什么? how can I make it like facebook when even if I trun off my computer and start it again it will remember my login info by coockis? 我怎么能像facebook一样,即使我从计算机上取下电脑并再次启动它也能记住我的登录信息?

Thanks 谢谢

index.html 的index.html

<html>
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-cookies.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', ['ngCookies']);
        app.controller('MyController', function ($scope, $window, $cookies) {
            $scope.WriteCookie = function () {
                $cookies.put("Name", $scope.Name);

            };
            $scope.ReadCookie = function () {
                $window.alert($cookies.get('Name'));
            };
            $scope.RemoveCookie = function () {
                $cookies.remove('Name');
            };
        });
    </script>
    <div ng-app="MyApp" ng-controller="MyController">
        Name:
        <input type="text" ng-model="Name" />
        <br />
        <br />
        <input type="button" value="Write Cookie" ng-click="WriteCookie()" />
        <input type="button" value="Read Cookie" ng-click="ReadCookie()" />
        <input type="button" value="Remove Cookie" ng-click="RemoveCookie()" />
    </div>
</body>
</html>

This is because if you don't set an expiration time for the cookie, it will be created as session cookie. 这是因为如果您没有为cookie设置过期时间,它将被创建为会话cookie。 Which will be removed as soon as you close the browser. 关闭浏览器后会立即将其删除。

In order to avoid this just set an expiration time for the cookie, like eg here for one day: 为了避免这种情况,只需设置cookie的过期时间,例如此处为一天:

var expireDate = new Date(); 
expireDate.setDate(expireDate.getDate() + 1); 

$cookies.put('Name', $scope.Name, {
  expires: expireDate
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM