无法使用AngularJS访问Cookie

时间:2015-12-06 15:38:45

标签: javascript angularjs cookies

我无法访问存储在localhost中的Cookie。

The cookis are already stored (view image)

When i try to display, i get undefined (view image)

这是我显示的js代码:

var app = angular.module("Authentification", ['ngCookies']);
app.controller("log", ['$cookieStore', '$scope', '$http', function($cookieStore, $scope, $http) {

    $scope.typeEmploye = $cookieStore.get('typeEmploye');
    alert($scope.typeEmploye);

}]);

1 个答案:

答案 0 :(得分:0)

这是我的js代码,其中我从我的其余API获取结果后将属性存储在cookie中。

var app = angular.module("Authentification", ['ngCookies']);
app.controller("main", ['$cookieStore', '$scope', '$http','$location',
function($cookieStore, $scope, $http, $location) {

$scope.user = [];
$scope.type=[];

$scope.auth = function() {
    $http.get("/Employe/authentification?login=" + $scope.log + "&password=" + $scope.pw)
        .success(function(data) {
            console.log(data);
            if (data) {
                $scope.user = data;
                $cookieStore.put('typeEmploye', $scope.user.type);
                $cookieStore.put('Authentified',true);


                $scope.type=$cookieStore.get('typeEmploye');
                if($scope.type == "gerant"){

                    window.location.href = "/Gerant/index.html";
                }
                else if($scope.type == "cuisinier"){

                    window.location.href = "/Cuisinier/index.html";
                }
                else if($scope.type == "servant"){

                    window.location.href = "/Servant/index.html";
                }
                else{
                    window.location.href = "/error";
                }

            }
            else{
                alert("Login ou mot de passe incorrects");
            }

        }).error(function(data, status) {
            alert("Problème dans le service d'authentification");

        });

};


}]);

信息存储在cookie中。但是,当我转到另一页(使用不同的js文件)时,我无法获取cookie。这是js代码。

var app = angular.module("ger", ['ngCookies']);
app.controller("main", ['$cookies', '$scope', '$http','$location',
function($cookies, $scope, $http, $location) {

var Type = $cookies.typeEmploye;
alert(Type);
}]);