Can anybody help me out from how to use $localStorage in angularjs, I can't find a complete guide for it.
I need explanation and syntax for usage such as,
localStorage.getItem();
localStorage.removeItem();
localStorage.clear();
localStorage.setItem();
Providing entire doc would help me well.
Save to localStorage:
localStorage.setItem("name", value);
Get from localStorage
var x = localStorage.getItem("name");
Remove from localStorage:
localStorage.removeItem("name");
Clear localStorage (remove all):
localStorage.clear();
Values in localStorage will be exists when you reopen your browser. When you need clear all when you close browser, you must use sessionStorage. Usage is same as localStorage
Related
window.onbeforeunload = function(){
window.localStorage.clear();
}
Here in this code the localstorage gets clear but on refresh page it clears the localStorage that i dont want.
Help me...
localStorage.clear(); will clear all the data from local storage.There is no need of page refresh to get the changes.
Example
Please provide a Verifiable example that produces the problem.
I am developing an angjs application.
Used angjs 1.6.
Following is my code to create a cookie
$cookies.put('globals', $rootScope.globals);
When i open a new tab, I dont find this cookie variable "globals".
How to share cookies across multiple tabs?
cookies are document specific. So, you are not getting in other tab. Please go for ngStorage or use localStorage
and one more thing is even you want to store non-prmitive data(object, array etc) in cookies/storage, you must stringfy before pushing and should be parsed after getting.
eg: $cookies.put('globals', JSON.stringify({name: 'My_name'}));
var myName = JSON.parse($cookies.get('globals'));
Check if this helps you out:
$cookies.put('globals', $rootScope.globals, {domain: 'yourdomainhere'});
If an user is coming from an specific page i need to do get some values out of a cookie and change what the users sees.
Now, the issue is that i cannot find a way to view what page the user is coming from.
EDIT: This is intended to capture when the users clicks back in a page and save the state of the previous page.
Any ideas?
Thanks in advance
Solved. Every time i load a page i'm saving the url, so when i get to this page i just have to read it to tell. Thanks!
You can use browser history in our javascript or you can write your last page in cookies and get the last link then update it
Using cookies will indeed fix this for you. So when a user goes to a new page - set a cookie like:
app.controller('myController',['$scope', '$location', $cookies], function($scope, $location, $cookies){
if($cookies.get('page') == '/index'){
//do stuff if user came from index
}
$scope.pageChanged = function(value){
$cookies.put('page', value);
$location.path('/index');
}
}
just make sure you use the pageChanged function to set your page every time user changes pages.
Using the $routeProvider you can use the resolve function to detect when a new route has been loaded.
Another way would be to listen for the event $routeChangeSuccessor $routeChangeError and get the information needed from the service $location or $route.
If you want a sample just ask me, I'll try to post one as soon as I have free time.
I'm using Angular v1.3.10 and i'm setting a cookie successfully.
When i'm doing $cookieStore.put(key,value) and then $cookieStore.get(key) i see the object I added.
When i'm refreshing the page and doing $cookieStore.get(key) i get undefined.
Any idea why ?
At first appearance seems correct.
This works for me (version 1.3.0):
$cookieStore.put('MyVar', $scope.myVar);
// After refresh
$scope.myVar = $cookieStore.get('MyVar');
On the other hand, you can see in documentation that $cookieStore is deprecated. You must use $cookies. It's very similar.
Good luck!
I store some keys in cache using angularJs cachefactory
myApp.factory('SomeCache', function ($cacheFactory) {
return $cacheFactory('someCache', {
capacity: 10 // optional - turns the cache into LRU cache
});
});
in the code :
SomeCache.put(key, value);
I see all the keys and values in the console.
The problem is , when i redirect to another page, not in my webSite ( e.c www.ebay.com)
and the i come back the cache object is empty,
I needd to preserve the data on the page.
Any idea ?
ishay
The behaviour you see with $cacheFactory is expected. The AngularJS-Cache does not persist between page refreshes. See also: Angularjs: date expiration for $cacheFactory
What you need to use to persist data between page refreshes is localStorage or sessionStorage as explained on this site: DOM Storage guide
You should use the excellent ngStorage module.