0

When user open user.html(user route) need to store(this should be avilable in entire application) localStorage.setItem('user',"true");

when user close user.html(means user close this particular tab(url/route) in browser) need to remove localstorage .how can i achieve this help me out to move forward

below is my code

var app = angular.module('Test', ['ngResource', 'ngRoute']);  

app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider){

    $locationProvider.hashPrefix('');

    $routeProvider

        .when('/', {

            templateUrl: 'home.html'

        })
        .when('/user', {

            templateUrl: 'user.html',

            controller: 'User'

        })
        .otherwise({

            redirectTo: '/'

        });

}]);

app.controller('User', function($scope) {
    localStorage.setItem('user',"true");
});
its me
  • 524
  • 6
  • 28
  • 62
  • 2
    Why not use sessionStorage instead? There is no need to destroy data on tab closing for it - https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage – Sahil Sharma May 29 '18 at 11:46
  • if i use sessionStorage it is not available in entire application – its me May 29 '18 at 12:16
  • You mean you want to destroy the data *only once every tab is closed* with your application? – Patrick May 29 '18 at 13:33
  • only when user open this user page we need to store value in localstorage but that should be avilable in entire application but once user closed this page we need to remove from localstorage @Patrick – its me May 29 '18 at 13:57
  • Duplicate of https://stackoverflow.com/q/3888902/116249 in that case? – Patrick May 29 '18 at 13:59
  • can you help me out move forward @Patrick – its me May 29 '18 at 14:11
  • @itsme Just register the event and clear the item from localStorage when the page unloads. This question asks the same thing for angularjs: https://stackoverflow.com/a/30345961/116249 What about those solutions don't you understand? – Patrick May 29 '18 at 14:35

0 Answers0