Javascript 如何避免刷新页面或更改窗口位置时删除$cookies?

Javascript 如何避免刷新页面或更改窗口位置时删除$cookies?,javascript,html,angularjs,cookies,Javascript,Html,Angularjs,Cookies,我试图在前端维持会话。但是,我发现每当我刷新浏览器或更改window.location时。饼干不见了 angular .module('myservices') .factory('myAuthentication', Authentication); Authentication.$inject = ['$cookies', '$http', '$q', '$rootScope']; function Authentica

我试图在前端维持会话。但是,我发现每当我刷新浏览器或更改
window.location
时。饼干不见了

      angular
        .module('myservices')
        .factory('myAuthentication', Authentication);

      Authentication.$inject = ['$cookies', '$http', '$q', '$rootScope'];

      function Authentication($cookies, $http, $q, $rootScope){

        var Authentication = {
          login                    : login,
          getAuthenticatedAccount  : getAuthenticatedAccount,
          isAuthentiate            : isAuthentiate,
          setAuthenticateAccount   : setAuthenticateAccount,
          unAuthenticate           : unAuthenticate,
        };

        return Authentication;

        function login(email, password){
          $http.post('login/', {
            email: email, password: password
          }).then(loginSuccess, loginError);
          function loginSuccess(response){

            Authentication.setAuthenticateAccount(response.data);

            $rootScope.$broadcast('login', "login");

            // changing window location will delete the cookies
            window.location = '/';
          }
          function loginError(response){
            console.log('error');
          }
        }

        function getAuthenticatedAccount(){
          if (!$cookies.authenticatedAccount){
            return ;
          }
          return JSON.parse($cookies.authenticatedAccount);
        }

        function isAuthentiate(){
          return !!$cookies.authenticatedAccount;
        }

        function setAuthenticateAccount(account){
          $cookies.authenticatedAccount = JSON.stringify(account);
        }

      }
我在firefox和safari中测试了这一点,并确保在firefox中测试了unchecked和disable cache

angular中是否有任何其他设置可以保持cookie的持久性?

您可以尝试使用“expires”属性,该属性表示何时使cookie过期,请查找以下代码,通过这些代码可以设置cookie过期时间:

美元饼干(


))

AngularJS的哪个版本?版本1.4+建议使用。除此之外,还有
putObject
getObject
API,它们应该不需要使用JSON.stringify和parse。
"name", 
"value", 
{ 
    // The "expires" option defines how many days you want the cookie active. The default value is a session cookie, meaning the cookie will be deleted when the browser window is            closed.
    expires: 7, 
    // The "path" option setting defines where in your site you want the cookie to be active. The default value is the page the cookie was defined on.
    path: '/', 
    // The "domain" option will allow this cookie to be used for a specific domain, including all subdomains (e.g. example.com). The default value is the domain of the page where the cookie was created.
    domain: 'example.com', 
    // The "secure" option will make the cookie only be accessible through a secure connection (like https://)
    secure: true 
}