1

I have a full fledged angular app.

The only functionality I need is if the same site is open in 4 tabs and if user is pressing logout button in one of the opened tabs it should logout the application in all other opened tabs.

I have found a beautiful example on this

function storageChange(event) {
    if(event.key == 'logged_in') {
        alert('Logged in: ' + event.newValue);
    }
}

window.addEventListener('storage', storageChange, false);

If, for example, one of the tabs logs out:

window.localStorage.setItem('logged_in', false);

But only problem is that I want to do it in angular not in plain javascript. Reason is I have my logout function written in angular so it will be bit difficult to merge both and another reason is that doing this in JS will surely make my code dirty.

So, Is there any way that I can use $watch or something to catch the localstorage change event.Or anything and then perform logout from all tabs.

Can anyone please convert the above code in Angular.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
AngularDev
  • 144
  • 1
  • 10
  • This is not what i required it is not working on cross tab changes. Please understand my question before marking it duplicate – AngularDev Dec 16 '14 at 11:58
  • 1
    I agree, the duplicate I suggested doesn't work with cross tab communication which is specifically what the OP was asking for. This should be reopened. – DoctorMick Dec 16 '14 at 15:36
  • Hang on, the second answer probably does answer this question! – DoctorMick Dec 16 '14 at 16:05

0 Answers0