Javascript 如何在AngularJS服务中管理cookie值?

Javascript 如何在AngularJS服务中管理cookie值?,javascript,angularjs,cookies,Javascript,Angularjs,Cookies,AngularJS服务从对后端REST服务的调用接收cookie。cookie值用作变量的值,因此该变量的值可以在AngularJS应用程序中的其他模块重复重新初始化服务时保持不变,只要不再调用REST服务来更改cookie的值。我阅读了文档,但文档中没有包含更改现有cookie值的方法。文档也没有对初始化服务模块中cookie派生变量的值进行注释 如何修改下面的代码来管理服务生命周期中cookie值的更改,以便在后端REST调用指示时进行更改,但在重新初始化AngularJS服务后,cooki

AngularJS服务从对后端REST服务的调用接收cookie。cookie值用作变量的值,因此该变量的值可以在AngularJS应用程序中的其他模块重复重新初始化服务时保持不变,只要不再调用REST服务来更改cookie的值。我阅读了文档,但文档中没有包含更改现有cookie值的方法。文档也没有对初始化服务模块中cookie派生变量的值进行注释

如何修改下面的代码来管理服务生命周期中cookie值的更改,以便在后端REST调用指示时进行更改,但在重新初始化AngularJS服务后,cookie值仍然存在?

以下是我迄今为止开发的代码:

cookie由Spring RestController在后端服务器上生成,并随响应一起发送,如下所示:

response.addCookie(new Cookie("keyName", "oneValue"));
someVariable : cookies.get('keyName'),
someModule.someVariable = $cookies.put('keyName', 'oneValue');

else:  

someModule.someVariable = $cookies.put('keyName', 'anotherValue');
angular.module('someModule', ['ngCookies']).factory( 'someModule', function($rootScope, $http, $location, $cookies) {

    var someModule = {

        someVariable : cookies.get('keyName'),//populate variable

        someMethod : function(funcJSON, callback) {
            $http.post('/rest-service-url', funcJSON).then(function(response, $cookies) {
                if(response.data.content=='whatweexpect'){
                    someModule.someVariable = $cookies.put('keyName', 'oneValue');//DOES THIS CHANGE VALUE OF COOKIE?
                    callback && callback(someModule.someVariable);
                }else {
                    someModule.someVariable = $cookies.put('keyName', 'anotherValue');//DOES THIS CHANGE VALUE OF COOKIE?
                    callback && callback(false);
                }
             });
        },

        init : function() {
            //some unrelated stuff
        }
    };
    return someModule;
});
<div ng-show="someVariable=='oneValue'">
    <p>oneValue says we give this content.</p>
</div>
<div ng-show="someVariable=='anotherValue'">
    <h1>anotherValue says we give this other content.</h1>
</div>
<div ng-show="someVariable==null">
    <h1>Assume the backend REST service has never been called, so give default content.</h1>
</div>
(来自服务器的响应还包括一个JSON对象,您可以在下面的
http.post(..
中看到)

在客户端AngularJS应用程序中,声明变量并在AngularJS模块代码顶部填充cookie值,如下所示:

response.addCookie(new Cookie("keyName", "oneValue"));
someVariable : cookies.get('keyName'),
someModule.someVariable = $cookies.put('keyName', 'oneValue');

else:  

someModule.someVariable = $cookies.put('keyName', 'anotherValue');
angular.module('someModule', ['ngCookies']).factory( 'someModule', function($rootScope, $http, $location, $cookies) {

    var someModule = {

        someVariable : cookies.get('keyName'),//populate variable

        someMethod : function(funcJSON, callback) {
            $http.post('/rest-service-url', funcJSON).then(function(response, $cookies) {
                if(response.data.content=='whatweexpect'){
                    someModule.someVariable = $cookies.put('keyName', 'oneValue');//DOES THIS CHANGE VALUE OF COOKIE?
                    callback && callback(someModule.someVariable);
                }else {
                    someModule.someVariable = $cookies.put('keyName', 'anotherValue');//DOES THIS CHANGE VALUE OF COOKIE?
                    callback && callback(false);
                }
             });
        },

        init : function() {
            //some unrelated stuff
        }
    };
    return someModule;
});
<div ng-show="someVariable=='oneValue'">
    <p>oneValue says we give this content.</p>
</div>
<div ng-show="someVariable=='anotherValue'">
    <h1>anotherValue says we give this other content.</h1>
</div>
<div ng-show="someVariable==null">
    <h1>Assume the backend REST service has never been called, so give default content.</h1>
</div>
然后在模块的方法中的两个不同点填充或更改cookie的值,具体取决于尝试调用后端的结果,如下所示:

response.addCookie(new Cookie("keyName", "oneValue"));
someVariable : cookies.get('keyName'),
someModule.someVariable = $cookies.put('keyName', 'oneValue');

else:  

someModule.someVariable = $cookies.put('keyName', 'anotherValue');
angular.module('someModule', ['ngCookies']).factory( 'someModule', function($rootScope, $http, $location, $cookies) {

    var someModule = {

        someVariable : cookies.get('keyName'),//populate variable

        someMethod : function(funcJSON, callback) {
            $http.post('/rest-service-url', funcJSON).then(function(response, $cookies) {
                if(response.data.content=='whatweexpect'){
                    someModule.someVariable = $cookies.put('keyName', 'oneValue');//DOES THIS CHANGE VALUE OF COOKIE?
                    callback && callback(someModule.someVariable);
                }else {
                    someModule.someVariable = $cookies.put('keyName', 'anotherValue');//DOES THIS CHANGE VALUE OF COOKIE?
                    callback && callback(false);
                }
             });
        },

        init : function() {
            //some unrelated stuff
        }
    };
    return someModule;
});
<div ng-show="someVariable=='oneValue'">
    <p>oneValue says we give this content.</p>
</div>
<div ng-show="someVariable=='anotherValue'">
    <h1>anotherValue says we give this other content.</h1>
</div>
<div ng-show="someVariable==null">
    <h1>Assume the backend REST service has never been called, so give default content.</h1>
</div>
模块的完整代码如下所示:

response.addCookie(new Cookie("keyName", "oneValue"));
someVariable : cookies.get('keyName'),
someModule.someVariable = $cookies.put('keyName', 'oneValue');

else:  

someModule.someVariable = $cookies.put('keyName', 'anotherValue');
angular.module('someModule', ['ngCookies']).factory( 'someModule', function($rootScope, $http, $location, $cookies) {

    var someModule = {

        someVariable : cookies.get('keyName'),//populate variable

        someMethod : function(funcJSON, callback) {
            $http.post('/rest-service-url', funcJSON).then(function(response, $cookies) {
                if(response.data.content=='whatweexpect'){
                    someModule.someVariable = $cookies.put('keyName', 'oneValue');//DOES THIS CHANGE VALUE OF COOKIE?
                    callback && callback(someModule.someVariable);
                }else {
                    someModule.someVariable = $cookies.put('keyName', 'anotherValue');//DOES THIS CHANGE VALUE OF COOKIE?
                    callback && callback(false);
                }
             });
        },

        init : function() {
            //some unrelated stuff
        }
    };
    return someModule;
});
<div ng-show="someVariable=='oneValue'">
    <p>oneValue says we give this content.</p>
</div>
<div ng-show="someVariable=='anotherValue'">
    <h1>anotherValue says we give this other content.</h1>
</div>
<div ng-show="someVariable==null">
    <h1>Assume the backend REST service has never been called, so give default content.</h1>
</div>
然后,
ng show
使用cookie的值来确定在视图中向用户显示的内容,如下所示:

response.addCookie(new Cookie("keyName", "oneValue"));
someVariable : cookies.get('keyName'),
someModule.someVariable = $cookies.put('keyName', 'oneValue');

else:  

someModule.someVariable = $cookies.put('keyName', 'anotherValue');
angular.module('someModule', ['ngCookies']).factory( 'someModule', function($rootScope, $http, $location, $cookies) {

    var someModule = {

        someVariable : cookies.get('keyName'),//populate variable

        someMethod : function(funcJSON, callback) {
            $http.post('/rest-service-url', funcJSON).then(function(response, $cookies) {
                if(response.data.content=='whatweexpect'){
                    someModule.someVariable = $cookies.put('keyName', 'oneValue');//DOES THIS CHANGE VALUE OF COOKIE?
                    callback && callback(someModule.someVariable);
                }else {
                    someModule.someVariable = $cookies.put('keyName', 'anotherValue');//DOES THIS CHANGE VALUE OF COOKIE?
                    callback && callback(false);
                }
             });
        },

        init : function() {
            //some unrelated stuff
        }
    };
    return someModule;
});
<div ng-show="someVariable=='oneValue'">
    <p>oneValue says we give this content.</p>
</div>
<div ng-show="someVariable=='anotherValue'">
    <h1>anotherValue says we give this other content.</h1>
</div>
<div ng-show="someVariable==null">
    <h1>Assume the backend REST service has never been called, so give default content.</h1>
</div>

oneValue表示我们提供此内容

另一个值表示我们提供其他内容。 假设从未调用过后端REST服务,那么请提供默认内容。

为了通过从初始化、重复调用后端REST服务到重复重新初始化的所有更改来管理cookie的值,我对上述代码做了哪些具体更改?

$http.post('/REST service url',funcJSON)。然后(函数(response,$cookies){
不应该需要$cookie作为函数的参数,因为$http服务不返回cookie。如果您
console.log
它,我假设它将是另一个对象或未定义。@ryan0319感谢您仔细查看。这部分是伪代码,以便在SO帖子中更清楚地显示它。我在帖子顶部添加了一行,显示cookie在Spring REST控制器中创建并连接到
响应
。REST服务在
响应
中发送JSON对象和cookie。因此
http.post(…
在AngularJS中,代码需要同时处理JSON和cookie。JSON处理得很好,但我不知道如何处理cookie。这有助于隔离问题吗?[该变量的值可以在AngularJS应用程序中的其他模块重复重新初始化服务时保持不变]。我不明白?一个服务只由angular初始化一次。只有重新加载页面才能使其重新初始化。因此我不明白cookie的需要。@Walfrat我的授权服务在用户重新加载安全路由时重新初始化布尔属性的值。因此,用户成功地进行身份验证并重定向转到安全页面并查看安全内容。但随后用户在浏览器中重新加载安全路由,并将值重新设置为false,以便用户注销。另一位用户建议使用基于cookie的方法,因此我发表此文章。下面是另一个链接: