6ffeog
Last Updated: February 25, 2016
·
29.78K
· bleakgadfly

Using ngCookies in AngularJS

The documentation on ngCookies in Angular is pretty vague so here is a little write-up how to get started with ngCookies.

For some reason, Angular 1.0.4 does not seem to include the ngCookies module so I had to download the cookies module manually and include it in my project. Note that if you just include the URL for the raw data on Github it won't work on IE because of a MIME mismatch.

In your App Module statement, add 'ngCookies' as a dependency:

var App = angular.module('myModuleName', ['ngCookies']);

Add the $cookies service as a parameter to the controller you want it in and access the cookie properties as a regular object

function SomeFunc($scope, $cookies) {
        $cookies.NameOfMyCookie = "Setting a value";
        alert($cookies.NameOfMyCookie);
}

Gist

2 Responses
Add your response

8192

This is secure to use in productions app?

over 1 year ago ·
8407

Depends on your use, I guess.

over 1 year ago ·