简体   繁体   中英

How to set cookie with angular

I trying to set a cookie following this approach . However I must have missed something fundamental as the console only returns undefined. Here is the full html content.

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular-cookies.js"></script>

<script>
    angular.module('App', ['ngCookies'])
    .controller('ctrl', ['$cookies', function($cookies) {
        $cookies.put('token', 'Test', {'expires': 'Fri, 31 Dec 9999 23:59:59'});
        console.log($cookies.get('token'));
    }]);
</script>

</head>
<body ng-app="App" ng-controller="ctrl"></body>
</html>

For the off-case it matters: Chromium 65 on Ubuntu 16.04 (64-bit). Thank you for your help.

Nothing wrong in your code with the way to set cookies. Actually Chrome ignores cookies from local pages but other browsers like safari will satisfy you with it's execution. If you upload the page to remote server , it will work for chromium. So It is better use localStorage instead of cookies in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM