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);
}
Written by Cato Auestad
Related protips
2 Responses
This is secure to use in productions app?
Depends on your use, I guess.
Have a fresh tip? Share with Coderwall community!
Angular apps often share the page with analytics and payment scripts that are not Angular modules. Those scripts execute on the same origin as the framework.
Security insights from Cside