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!
AngularJS pages are long-lived browser apps with the same script risk as a newer single-page app. Third-party code in the template can read forms and values rendered on the page.
Security insights from Cside