使用 AngularJS 更新 cookie

Update cookies with AngularJS

我是 AngularJS 的菜鸟。我正在尝试管理 cookie,但它无法正常工作。我有两个版本的代码接缝相同,但如果第一个有效......第二个失败。

我的登录表单代码在这里:

(function() {
    var app = angular.module('userApp', ['suiviChantiers', 'ngCookies']);

    app.controller('UserLogin', ['$scope', '$http', '$cookies',
        function($scope, $http, $cookies) {

          $scope.login = function() {
            $scope.user = this.user;

            $http.post('services/user.php', {
              action: 'login',
              user: $scope.user
            }).success(function(data) {
              if (data.error) {
                // [...]
              } else if (data.message == 'success') {
                $cookies.user = data.data;
                console.log("User : " + $cookies.user); // Output is : User : MS1OVFEwWkRZeU5ETTBNVFV3TVE9PQ==
                window.setTimeout(function() {
                  if ($cookies.redirectTo == null) {
                    document.location.href = "chantier/index.html";
                  } else {
                    var dest = $cookies.redirectTo;
                    $cookies.redirectTo = null;
                    document.location.href = dest;
                  }
                }, 3000);
              }
            });
          };
          // [...]
        })();
    })();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-cookies.min.js"></script>
<h1>Login form ...</h1>

在我的 "chantier/index.html" 控制器中我有这个 :

(function() {
 var app = angular.module('chantierApp', ['ngCookies']);
 
 app.controller('ChantierListController', ['$scope', '$cookies', 'userService', function($scope, $cookies, userService) {
  $scope.user = userService.user;
  console.log("User : "+$cookies.user); // Output is : User : undefined but $cookies.redirectTo is not empty

 }]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-cookies.min.js"></script>

(抱歉代码这么长post) 你能解释一下为什么 cookie 没有更新吗?

感谢您的帮助。

终于解决了我的问题...一切正常。但是某些文件上有一些非常非常持久的缓存(即使在删除缓存文件和 tmp 文件之后)。 我以私人模式启动浏览器,一切正常。

感谢您的帮助。