How to trigger an Observable in Angular 2 on local storage change

0 votes
Can i know How to trigger an Observable in Angular 2 on local storage change?
Mar 11 in Node-js by Ashutosh
• 23,230 points
57 views

1 answer to this question.

0 votes

In Angular 2+, you can trigger an Observable on localStorage changes using the StorageEvent and BehaviorSubject.

Solution:

Create a Service to Watch localStorage Changes

import { Injectable } from '@angular/core';

import { BehaviorSubject } from 'rxjs';

@Injectable({ providedIn: 'root' })

export class StorageService {

  private storageSub = new BehaviorSubject<string | null>(localStorage.getItem('key'));

  get storageChanges$() {

    return this.storageSub.asObservable();

  }

  setItem(key: string, value: string) {

    localStorage.setItem(key, value);

    this.storageSub.next(value);

  }

}


Listen for Storage Changes in Another Component

import { Component, HostListener } from '@angular/core';

import { StorageService } from './storage.service';

@Component({

  selector: 'app-root',

  templateUrl: './app.component.html',

})

export class AppComponent {

  constructor(private storageService: StorageService) {

    this.storageService.storageChanges$.subscribe(value => {

      console.log('Storage Updated:', value);

    });

  }

  @HostListener('window:storage', ['$event'])

  onStorageEvent(event: StorageEvent) {

    if (event.key === 'key') {

      this.storageService.storageChanges$.subscribe(value => console.log('Updated Value:', value));

    }

  }

}

answered Mar 11 by Tanmay

Related Questions In Node-js

0 votes
1 answer

How to get GET (query string) variables in Express.js on Node.js?

Hello @kartik, Since you've mentioned Express.js in your ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
3,396 views
0 votes
1 answer

How to write a test which expects an Error to be thrown in Jasmine?

Hello @kartik, Try using an anonymous function instead: expect( ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,840 points
9,799 views
0 votes
1 answer

How to create an HTTPS server in Node.js?

Hello @kartik, The minimal setup for an HTTPS ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,840 points
2,034 views
0 votes
1 answer

How to get access to webpack-dev-server from devices in local network?

Hello @kartik, You can set your ip address ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,840 points
6,409 views
0 votes
1 answer

How to Center a Single-Column Div Within a 12-Column Container in Bootstrap 3?

Using Bootstrap's Offset Classes Bootstrap 3 provides offset ...READ MORE

answered Mar 11 in Node-js by Tanvi
66 views
0 votes
1 answer
0 votes
1 answer

How to unsubscribe from an observable to prevent memory leaks in Angular?

In Angular, unsubscribe from observables to prevent ...READ MORE

answered Mar 21 in Node-js by Anvi
40 views
0 votes
1 answer

How to host MEAN stack application with Angular and nodejs on windows IIS

It's fine that you're using Angular. Be ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
1,500 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP