AngularJS cookies

Angular-cookies Doc:
https://docs.angularjs.org/api/ngCookies/service/$cookies

Download angular-cookies.js:
https://docs.angularjs.org/api/ngCookies

https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-cookies.js

controller.js

var myApp = angular.module('myApp', ['ngCookies']);
myApp.controller('ColorCtrl', ['$scope', '$cookies', function($scope, $cookies){  
  // Setting a cookie
  $cookies['myFavorite'] = 'oatmeal';
  // Retrieving a cookie
  $scope.favoriteCookie = $cookies['myFavorite'];
}]);

index.html

 <!doctype html>
<html ng-app="myApp">
 <head>
 <meta http-equiv="Content-Type">
 <meta content="text/html; charset=utf-8">
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

 <script src="angular.js"></script>
 <script src="angular-cookie.js"></script>
 <script src="controllers.js"></script>
 </head>
 <body ng-controller='ColorCtrl'>
 {{ favoriteCookie }}
 </body>
</html>

3 thoughts on “AngularJS cookies

Leave a Reply

Your email address will not be published. Required fields are marked *