11+ Year IT Industry Experience, Working as Technical Lead with Capgemini | Consultant | Leadership and Corporate Trainer | Motivational and Technical Speaker | Career Coach | Author | MVP | Founder Of RVS Group | Trained more than 4000+ IT professionals | Azure | DevOps | ASP.NET | C# | MVC | WEB API | ANGULAR | TYPESCRIPT | MEAN | SQL | SSRS | WEB SERVICE | WCF... https://bikeshsrivastava.blogspot.in/ http://bikeshsrivastava.com/

How to save information on client side using Angularjs?

Today we are implementing exceptionally straightforward case of how to save object value or any information on client side using in AngularJS.There are many options to store information.
1:-LocalStorage:-
setitem:- localStorage.setItem('history',JSON.stringify(oldItems));
// olditem ==Your object value or information
getitem :-
localStorage.getItem('history');
//history is key
Remove item:-
$scope.RemoveHistory = function (item,index) {
var historyData = localStorage.getItem('history');
var parsedData = JSON.parse(historyData);
for (i = 0; i < parsedData.length; i++) {
//check condition (optional)
if (parsedData[i].UserName === item.UserName && parsedData[i].source === item.source && parsedData[i].destination === item.destination)
{
parsedData.splice(i, 1);
//Remove item using index and set again in same key "history".
localStorage["history"] = JSON.stringify(parsedData);
}
}

2:- Same like this you can use "sessionstorage" :-
sessionStorage.setItem('key', 'value'); 
var data = sessionStorage.getItem('key');
3:- Window storage :-

window.localStorage.setItem("key", Value);
var data=window.localStorage.getItem("UserToken");
4:-Cookies storage
angular.module('cookiesExample', ['ngCookies']) .controller('ExampleController', ['$cookies', function($cookies) { 
// Retrieving a cookie 
var favoriteCookie = $cookies.get('myFavorite'); 
// Setting a cookie 
$cookies.put('myFavorite', value); 
}]);
Use $cookiesProvider to change the default behavior of the $cookies service.

Note :-Don't forget to add respective directive for storage option.best option is localstorae than other.

localStorage vs. sessionStorage vs. Cookies:-

As far as abilities, Cookies just permit you to store strings. sessionStorage and localStorage permit you to store JavaScript primitives yet not Objects or Arrays (it is conceivable to JSON serialize them to store them utilizing the APIs). Session stockpiling will by and large permit you to store any primitives or items upheld by your Server Side dialect/system.
You have just read an article that categorized by title Angular / AngularJs by title How to save information on client side using Angularjs?. You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/06/part-19how-to-save-information-on.html. Thank You!
Author: Bikesh Srivastava - Monday, June 20, 2016

2 comments to "How to save information on client side using Angularjs?"

Life Is Complicated, But Now programmer Can Keep It Simple.