Remove local storage data when browser/tab close in angularjs - angularjs

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.

Related

How does the $localStorage in angularjs works?

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

[AngularJS]: Reloading Page without Returning to First State

I tried to follow this approach
While it does reload, it does not however allow me to stay on my current page. It keeps on going back to the first state of my app.
I am currently using ui-router. Is there another way of reloading the page without going back to the first state? I am currently experiencing an odd flicker when I do this:
$scope.submit = function() {
$state.go($state.current, {}, {
reload: true
});
doDeposit();
}
and then pass parameters on it (within doDeposit()):
var oResponse = data;
$state.transitionTo('deposit.status', {
opts: {
'response': oResponse,
'amount': data.amount
}
});
Current behavior: Page reloads; goes back to the home page for a second, then switches back to the intended page. I just need the data to refresh within its page. I have a 'submit again' button that redo the initial behavior of the first 'submit'. It re-processes the data available without going back to its initial state.
Hope this can help you solve your problem. Good luck to you!
Set URL to SEO Friendly Title with Dashes instead of ID
i'm sorry because first answer i'm copy wrong link. Hope this can help you:
https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/

Get the url that a user is coming from AngularJS

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.

$cookieStore doesn't keep value after page refresh

I'm a beginner with AngularJs and i have some trouble understanding how to use $cookieStore :/
I have a lot of buttons. Everytime a button is clicked, a distinct function is called in the controller, and in this function, i'm trying to store a value in a cookie
example :
$cookieStore.put('cookie', '1');
And at the loading of the page, i added this line :
alert($cookieStore.get('cookie'));
When we load the page for the first time, it's normal to get an "undefined" popup. But the problem is, even after clicking multiple buttons, i always got an undefined popup after refresh.
Here's a working demo : http://plnkr.co/edit/6kuqaT7ISpo7uEwLHcZn?p=preview
Please help
Thanks
I checked your plunker cookie is setting properly :-)
Use STOP AND RUN button for refresh :P
I saw similar problem with refresh + $cookieStore some time ago, try using sessionStorage, if your target browser(s) allow that.
What Angular version do you use?

Code changes not taking effect until refresh/reload is clicked

Any time I make changes to the code in my angularjs site, they do take effect until the user clicks the refresh button. Even if I log out to a page that is not angular then log back in it is the same old code until I click the refresh button. So if someone logs in it is the same as the last time they logged in regardless of changes and I obviously can't expect them to click refresh every time they log in.
So my question is how could I force a refresh of the code. I don't want to use window.reload or anything like that if there is a speacial angular way to accomplish this. I have tried clearing the template cache but it doesn't work. This is what I tried:
.run(function($rootScope, $templateCache) {
$templateCache.removeAll();
$rootScope.$on('$routeChangeStart', function(event, next, current) {
if (typeof(current) !== 'undefined'){
$templateCache.remove(current.templateUrl);
}
});
})
My problem turn out to be the browser caching js files. I didn't think that would be the problem because prior to using angularjs I never had this problem. So to fix the issue I found this: How to force browser to reload cached CSS/JS files? and modified it a little so I didnt have to use all the url rewriting.
I added the php function
function get_version($file){
return filemtime($_SERVER['DOCUMENT_ROOT'] . $file);
}
Then where I link my scripts I use:
<script src="/path/to/script.js?<?php echo get_version('/path/to/script.js');?>"></script>

Resources