Create cookie with AngularJS

纵饮孤独 提交于 2019-11-29 00:28:47

It happends via setting the $cookies variable:

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

Your version:

angular.module('myApp', ['ngCookies'])
.controller('myController', ['$scope', '$http','$cookies', function ($scope, $http, $cookies) {

  // Retrieving a cookie
  var favoriteCookie = $cookies.myFavorite;
  // Setting a cookie
  $cookies.myFavorite = 'oatmeal';
}]);

Source


NOTE: Remember to include <script src="angular-cookies.js"> in your html.

You must inject ngCookies in your module:

angular.module('myApp', ['ngCookies'])
Pulithevan S

Missing ngcookies in your module

angular.module('myApp', ['ngCookies'])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!