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应用。
whenever I enter this (via nodeJS) and go to this url: http://127.0.0.1:8080
每当我输入这个(通过nodeJS)并访问这个url: http://127.0.1:8080
the cookies is presisted in the browser's cookies. when I click refresh or close the tab and opening a new tab with this url (without closing the browser) the cookies stays.
饼干是在浏览器的饼干里保存的。当我点击刷新或关闭标签,并打开一个新的标签与这个url(不关闭浏览器),cookies留下。
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>
1 个解决方案
#1
3
This is because if you don't set an expiration time for the cookie, it will be created as session cookie. Which will be removed as soon as you close the browser.
这是因为如果您没有为cookie设置过期时间,那么它将被创建为会话cookie。当您关闭浏览器时,它将被删除。
In order to avoid this just set an expiration time for the cookie, like e.g. here for one day:
为了避免出现这种情况,只需为cookie设置一个过期时间,例如:
var expireDate = new Date();
expireDate.setDate(expireDate.getDate() + 1);
$cookies.put('Name', $scope.Name, {
expires: expireDate
});
#1
3
This is because if you don't set an expiration time for the cookie, it will be created as session cookie. Which will be removed as soon as you close the browser.
这是因为如果您没有为cookie设置过期时间,那么它将被创建为会话cookie。当您关闭浏览器时,它将被删除。
In order to avoid this just set an expiration time for the cookie, like e.g. here for one day:
为了避免出现这种情况,只需为cookie设置一个过期时间,例如:
var expireDate = new Date();
expireDate.setDate(expireDate.getDate() + 1);
$cookies.put('Name', $scope.Name, {
expires: expireDate
});