In this article I will disclose how to utilize Browser Cookies in AngularJS i.e. perusing esteems put away in Cookies, composing (sparing) values in Cookies and furthermore how to erase Cookies utilizing AngularJS.
AngularJS utilizes ngCookies module and $cookieStoreservice to complete the different elements of perusing, composing and evacuating Cookies.
AngularJS Cookies
Read, Write (Save) and Remove (Delete) Cookies
The beneath HTML Markup comprises of a HTML DIV to which ng-application and ng-controller AngularJS orders have been doled out.
Alongside the AngularJS JavaScript record, rakish cookies.js document additionally should be acquired keeping in mind the end goal to perform operations on Browser Cookies.
The accompanying AngularJS App makes utilization of ngCookies module and $cookieStore benefit.
Write Cookies
At the point when the Write Cookie Button is clicked, the WriteCookie capacity of the controller gets called which spares the estimation of the TextBox to a Cookie utilizing the $cookieStore administration of the ngCookies module.
The $cookieStore put work has two parameters, first the Name (Key) of the Cookie and second the Value to be put away in the Cookie.
Read Cookies
At the point when the Read Cookie Button is clicked, the ReadCookie capacity of the controller gets called which brings the estimation of the Cookie utilizing the $cookieStore administration of the ngCookies module.
The $cookieStore get work acknowledges the Name (Key) of the Cookie with a specific end goal to peruse its esteem.
Remove Cookies
At the point when the Remove Cookie Button is clicked, the RemoveCookie capacity of the controller gets called which expels the Cookie utilizing the $cookieStore administration of the ngCookies module.
The $cookieStore evacuate work acknowledges the Name (Key) of the Cookie with a specific end goal to expel the Cookie.
- <html>
-
- <head>
- <title></title>
- </head>
-
- <body>
- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular-cookies.js"></script>
- <script type="text/javascript">
- var app = angular.module('MyApp', ['ngCookies']);
- app.controller('Ctrl1', function($scope, $cookieStore) {
-
- $scope.EmpId = function() {
- $cookieStore.put("EmpId", $scope.EmployeeId);
- };
-
- $scope.ReadCookie = function() {
- var abc = $cookieStore.get('EmpId');
- };
-
- $scope.RemoveCookie = function() {
- $cookieStore.remove('EmpId');
- };
- });
- </script>
- <div ng-app="MyApp" ng-controller="Ctrl1"> Empoyee Id: <input type="text" ng-model="EmployeeId" /> <br /> <br /> <input type="button" value="Write Cookie" ng-click="WriteCookie()" /> <input type="button" value="Read Cookie" ng-click="ReadCookie()" /> <input type="button" value="Remove Cookie" ng-click="RemoveCookie()" /> </div>
- </body>
-
- </html>
Summary
I trust that amateurs and additionally understudies comprehend idea of Cookies in AngularJS with Example. Thanks for reading.