A Simple example of Get, Set and Clear Cookie in AngularJS
In this post we will give you information about A Simple example of Get, Set and Clear Cookie in AngularJS. Hear we will give you detail about A Simple example of Get, Set and Clear Cookie in AngularJSAnd how to use it also give you demo for it if it is necessary.
Angularjs provides ngCookies
modules to do this task so include angular-cookies.js file first before injecting ngCookies module into your application.
Using $cookies
service, you can read browser cookies, write values in browser cookies and delete browser cookies.
This Angularjs App consits of HTML textbox with ng-model directive and there are three button with ng-click directive.
- Set Cookies : When you click Set Cookies button then SetCookies function will be called and a new value entered by you will be saved into browser cookie using
put
method in key value format. - Get Cookies : When you click Get Cookies button then GetCookies function will be called and you will get value stored in cookies using get method that accept the name(key) of cookie.
- ClearCookies : When you click Clear Cookies button then ClearCookies function will be called that remove the cookie using remove method that accept the name(key) of cookie.
- <html>
- <head>
- <title>A Simple example of Get, Set and Clear Cookie in AngularJS</title>
- </head>
- <body>
- <scripttype="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
- <scripttype="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-cookies.js"></script>
- <scripttype="text/javascript">
- var app = angular.module('MyApp', ['ngCookies']);
- app.controller('CookiesController', function ($scope, $window, $cookies) {
- $scope.SetCookies = function () {
- $cookies.put("username", $scope.username);
- };
- $scope.GetCookies = function () {
- $window.alert($cookies.get('username'));
- };
- $scope.ClearCookies = function () {
- $cookies.remove('username');
- };
- });
- </script>
- <divng-app="MyApp"ng-controller="CookiesController">
- Username:
- <inputtype="text"ng-model="username"/>
- <br/>
- <br/>
- <inputtype="button"value="Set Cookies"ng-click="SetCookies()"/>
- <inputtype="button"value="Get Cookies"ng-click="GetCookies()"/>
- <inputtype="button"value="Clear Cookies"ng-click="ClearCookies()"/>
- </div>
- </body>
- </html>
- <scripttype="text/javascript">
- var app = angular.module('MyApp', ['ngCookies']);
- app.controller('CookiesController', function ($scope, $window, $cookieStore) {
- $scope.SetCookies = function () {
- $cookieStore.put("username", $scope.username);
- };
- $scope.GetCookies = function () {
- $window.alert($cookieStore.get('username'));
- };
- $scope.ClearCookies = function () {
- $cookieStore.remove('username');
- };
- });
- </script>
- <html>
- <head>
- <title>A Simple example of Get, Set and Clear Cookie using service factory in AngularJS</title>
- </head>
- <body>
- <scripttype="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
- <scripttype="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-cookies.js"></script>
- <scripttype="text/javascript">
- var app = angular.module('MyApp', ['ngCookies']);
- app.controller('CookiesController', function ($scope,userPersistenceService, $window) {
- $scope.SetCookies = function () {
- userPersistenceService.setCookieData($scope.username)
- };
- $scope.GetCookies = function () {
- $window.alert(userPersistenceService.getCookieData('username'));
- };
- $scope.ClearCookies = function () {
- userPersistenceService.clearCookieData();
- };
- });
- app.factory("userPersistenceService", [
- "$cookies", function($cookies) {
- var userName = "";
- return {
- setCookieData: function(username) {
- $cookies.put("username", username);
- },
- getCookieData: function() {
- userName = $cookies.get("username");
- return userName;
- },
- clearCookieData: function() {
- username = "";
- $cookies.remove("username");
- }
- }
- }
- ]);
- </script>
- <divng-app="MyApp"ng-controller="CookiesController">
- Username:
- <inputtype="text"ng-model="username"/>
- <br/>
- <br/>
- <inputtype="button"value="Set Cookies"ng-click="SetCookies()"/>
- <inputtype="button"value="Get Cookies"ng-click="GetCookies()"/>
- <inputtype="button"value="Clear Cookies"ng-click="ClearCookies()"/>
- </div>
- </body>
- </html>
$cookies
<html> <head> <title>A Simple example of Get, Set and Clear Cookie in AngularJS</title> </head> <body> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-cookies.js"></script> <script type="text/javascript"> var app = angular.module('MyApp', ['ngCookies']); app.controller('CookiesController', function ($scope, $window, $cookies) { $scope.SetCookies = function () { $cookies.put("username", $scope.username); }; $scope.GetCookies = function () { $window.alert($cookies.get('username')); }; $scope.ClearCookies = function () { $cookies.remove('username'); }; }); </script> <div ng-app="MyApp" ng-controller="CookiesController"> Username: <input type="text" ng-model="username" /> <br /> <br /> <input type="button" value="Set Cookies" ng-click="SetCookies()" /> <input type="button" value="Get Cookies" ng-click="GetCookies()" /> <input type="button" value="Clear Cookies" ng-click="ClearCookies()" /> </div> </body> </html>
$cookiesStore
$cookiesStore
feature is available in same angular-cookies.min.js file and use same get(), put() and remove() method.
<script type="text/javascript"> var app = angular.module('MyApp', ['ngCookies']); app.controller('CookiesController', function ($scope, $window, $cookieStore) { $scope.SetCookies = function () { $cookieStore.put("username", $scope.username); }; $scope.GetCookies = function () { $window.alert($cookieStore.get('username')); }; $scope.ClearCookies = function () { $cookieStore.remove('username'); }; }); </script>
Using service Factory
<html> <head> <title>A Simple example of Get, Set and Clear Cookie using service factory in AngularJS</title> </head> <body> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-cookies.js"></script> <script type="text/javascript"> var app = angular.module('MyApp', ['ngCookies']); app.controller('CookiesController', function ($scope,userPersistenceService, $window) { $scope.SetCookies = function () { userPersistenceService.setCookieData($scope.username) }; $scope.GetCookies = function () { $window.alert(userPersistenceService.getCookieData('username')); }; $scope.ClearCookies = function () { userPersistenceService.clearCookieData(); }; }); app.factory("userPersistenceService", [ "$cookies", function($cookies) { var userName = ""; return { setCookieData: function(username) { $cookies.put("username", username); }, getCookieData: function() { userName = $cookies.get("username"); return userName; }, clearCookieData: function() { username = ""; $cookies.remove("username"); } } } ]); </script> <div ng-app="MyApp" ng-controller="CookiesController"> Username: <input type="text" ng-model="username" /> <br /> <br /> <input type="button" value="Set Cookies" ng-click="SetCookies()" /> <input type="button" value="Get Cookies" ng-click="GetCookies()" /> <input type="button" value="Clear Cookies" ng-click="ClearCookies()" /> </div> </body> </html>
Show Demo
Hope this code and post will helped you for implement A Simple example of Get, Set and Clear Cookie in AngularJS. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs