File

src/app/services/storage.service.ts

Index

Properties
Methods

Constructor

constructor()

Methods

delete
delete(key: string)
Parameters :
Name Type Optional
key string no
Returns : void
deleteAll
deleteAll()
Returns : void
get
get(key: string)
Parameters :
Name Type Optional
key string no
Returns : string
getObject
getObject(key: string)
Parameters :
Name Type Optional
key string no
Returns : any
set
set(key: string, value: any)
Parameters :
Name Type Optional
key string no
value any no
Returns : void

Properties

storage
storage: Storage
Type : Storage
import { Injectable } from '@angular/core';

declare var window: any;

@Injectable()
export class StorageService {
  storage: Storage;
  constructor() {
    this.storage = window.sessionStorage;
  }
  get(key: string): string {
    return this.storage.getItem(key);
  }

  getObject(key: string): any {
    const value = this.storage.getItem(key);
    const obj = value && value.length > 0 ? JSON.parse(value) : null;
    return obj;
  }

  set(key: string, value: any): void {
    value = typeof value === 'string' ? value : JSON.stringify(value);
    return this.storage.setItem(key, value);
  }
  deleteAll(): void {
    this.storage.clear();
  }
  delete(key: string): void {
    this.storage.removeItem(key);
  }
}

results matching ""

    No results matching ""