javascript - Watch changes in local Storage in Angular 2 -


this question has answer here:

i have menu appear based on being logged. using json web tokens in local storage authentication, want watch change localstorage , re-update view on new information.

how can watch changes localstorage?

i set localstorage this

localstorage.setitem('jwt', my_token); things check if have token, if don't nothing happens, when there change fire event. know have create service, inject want , susbscribe me observable.

what have service, don't know how watch changes.

my service is:

import { injectable } 'angular2/core';  @injectable() export class localstorageservice {     private storage: any;      constructor() {         this.storage = localstorage;     }      public retrieve(key: string): {         var item = this.storage.getitem(key);          if (item && item !== 'undefined') {             return json.parse(this.storage.getitem(key));         }          return;     }      public store(key: string, value: any) {         this.storage.setitem(key, json.stringify(value));     }  } 

i've created service can if still haven't found suitable answer. h5webstorage handle watching both localstorage , sessionstorage events , synchronizes changes. have use normal object.


Comments