magu_
magu_

Reputation: 4866

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.

Upvotes: 0

Views: 2426

Answers (1)

Sunny
Sunny

Reputation: 615

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.

Upvotes: 1

Related Questions