Introduction
Local Storage is the awesome feature of the latest Browsers. The developer can use this feature to store some data into the Browser.
The data can be stored, using Key, value pair in the Local Storage and the value can be accessed, using Key.
The data can be stored, using Key, value pair in the Local Storage and the value can be accessed, using Key.
The syntax to store into Local Storage is given below.
- localStorage.setItem(likeKey, likeValue);
- var voting = localStorage.getItem(likeKey);
Write the code, given below into controller.
Save Likes.
Save Likes.
- controller.saveLikes = function (votingId) {
- var votingKey = votingId;
- var existingVoting = controller.getLikes(votingId);
- if (existingVoting === 'NaN' || existingVoting === null) {
- existingVoting = 0;
- }
- var vote = parseInt(existingVoting) + 1;
- localStorage.setItem(votingKey, vote);
- controller.getLikes(votingId);
- }
- controller.getLikes = function (votingId) {
- var votingKey = votingId;
- controller.getTotalLikes();
- var voting = localStorage.getItem(votingKey);
- if (voting === 'NaN' || voting === null)
- return 0;
- return voting;
- }
- controller.getTotalLikes = function () {
- controller.totalLikes = 0;
- for (var i = 0; i < localStorage.length; i++) {
- controller.totalLikes = controller.totalLikes + parseInt(localStorage.getItem(localStorage.key(i)));
- }
- }
- <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
- <div class="pull-right table-bordered"
- ng-click="homeController.saveLikes(nitroItem.name)" style="cursor: pointer;width:50px; height:40px; background-color:#ededed;">
- <div style="margin-top:8px; margin-left:8px;"><i class="fa fa-thumbs-o-up"></i> {{homeController.getLikes(nitroItem.name)}}</div>
- </div>
Conclusion
Local Storage provides the best way to store the data up to the local level. You can store the different types of data such as like counts, your wishlist, configuration settings etc.
Humayun Kabir MamunPosted Oct 13, 2016, 4:02 AM
Nice...