Skip to content

Commit b15a541

Browse files
committed
feb-15-Cache-CRUD-opration-AngularJs-1
1 parent 278d862 commit b15a541

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

dev/CRUD-Cache.html

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>User Login Cache</title>
7+
<!-- Google Hosted CDN -->
8+
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
9+
</head>
10+
<body ng-app="myApp">
11+
12+
<div ng-controller="AuthController">
13+
<h2>User Login</h2>
14+
<input type="email" ng-model="user.email" placeholder="Email">
15+
<input type="password" ng-model="user.password" placeholder="Password">
16+
<button ng-click="login()">Login</button>
17+
18+
<h3>Cached User Info</h3>
19+
<p>{{ cachedUser | json }}</p>
20+
21+
<button ng-click="logout()">Logout</button>
22+
</div>
23+
24+
<script>
25+
var app = angular.module('myApp', []);
26+
27+
app.factory('CacheService', function($cacheFactory) {
28+
var cache = $cacheFactory('authCache');
29+
30+
return {
31+
createCache: function(key, value) {
32+
cache.put(key, value);
33+
},
34+
getCache: function(key) {
35+
return cache.get(key);
36+
},
37+
updateCache: function(key, value) {
38+
cache.put(key, value);
39+
},
40+
deleteCache: function(key) {
41+
cache.remove(key);
42+
},
43+
clearAllCache: function() {
44+
cache.removeAll();
45+
}
46+
};
47+
});
48+
49+
app.controller('AuthController', function($scope, CacheService) {
50+
$scope.user = { email: "", password: "" };
51+
$scope.cachedUser = CacheService.getCache('user') || "No user logged in";
52+
53+
$scope.login = function() {
54+
if ($scope.user.email && $scope.user.password) {
55+
var userData = {
56+
email: $scope.user.email,
57+
token: "abcdef123456", // Mock token
58+
role: "admin", // Mock role
59+
modified: new Date().toISOString()
60+
};
61+
CacheService.createCache('user', userData);
62+
$scope.cachedUser = userData;
63+
} else {
64+
alert("Enter email and password");
65+
}
66+
};
67+
68+
$scope.logout = function() {
69+
CacheService.clearAllCache();
70+
$scope.cachedUser = "No user logged in";
71+
};
72+
});
73+
74+
</script>
75+
</body>
76+
</html>

0 commit comments

Comments
 (0)