bobbyg603 / ngx-localstorage

An Angular wrapper for localstorage/sessionstorage access.

Home Page:https://bohoffi.github.io/ngx-localstorage/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

npm version

All Contributors

PR-builder

ngx-localstorage

An Angular wrapper for localstorage/sessionstorage access.

Feel free to take a look at the DEMO / API.

Installation

Install via npm:

npm install ngx-localstorage

Usage

If you want to use the ngxLocalStorage directive you have to import it either in yopur module or component/directive.

Import LocalStorageDirective

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { LocalStorageDirective } from 'ngx-localstorage';

@NgModule({
  imports: [BrowserModule, LocalStorageDirective],
  bootstrap: [AppComponent]
})
export class AppModule {}

Configuration

For configuration provide it using the NGX_LOCAL_STORAGE_CONFIG InjectionToken or use the provideNgxLocalstorage() helper method (which optionally allows providing additional features as well).

  • prefix
    • Type: string?
    • Determines the key prefix.
    • Default: undefined
  • allowNull
    • Type: boolean?
    • Determines if null | 'null' values should be stored.
    • Default: true
  • storageType
    • Type: StorageType?
    • Determines the storage type.
    • Default: 'localStorage'
  • delimiter
    • Type: string?
    • Determines the delimiter in between prefix and key.
    • Default: underscore('_')

Serialization

Default serialization

The library utilizes the JSON.stringify()/JSON.parse() mechanics to pass values (of any type) to and from localstorage per default. If you wish you can override that behaviour by injecting your own custom serializer (app wide) or pass one per stoage call.

App wide serializer

Inject your custom serializer implentation using the provided injection token or pass it to the provideNgxLocalstorage() helper method:

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {NGX_LOCAL_STORAGE_SERIALIZER} from 'ngx-localstorage';

@NgModule({
    imports: [
        BrowserModule
    ],
    bootstrap: [AppComponent],
    providers: [
      {
        provide: NGX_LOCAL_STORAGE_SERIALIZER,
        useClass: <custom serializer implementing StorageSerializer>
      }
    ]
})
export class AppModule { }
Per call serializer

Every set()/get() call on LocalstorageService now supports to pass an optional (de)seriaizer. If none is provided the app wide (or default) one is used.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Basti Hoffmann
Basti Hoffmann

πŸ’» πŸ“– πŸ€” 🚧 πŸ“¦ πŸ‘€
Matt Lewis
Matt Lewis

🚧
Mark
Mark

πŸ› πŸ’‘
NewteqDeveloper
NewteqDeveloper

πŸ’‘
Abhishek Datta
Abhishek Datta

🚧
Mikael Syska
Mikael Syska

πŸ›
Vladimir Stempel
Vladimir Stempel

πŸ’»
assureclaims
assureclaims

πŸ’» 🎨 πŸ“–
Boris-Bodin
Boris-Bodin

🚧

This project follows the all-contributors specification. Contributions of any kind welcome!

About

An Angular wrapper for localstorage/sessionstorage access.

https://bohoffi.github.io/ngx-localstorage/

License:Apache License 2.0


Languages

Language:TypeScript 50.9%Language:HTML 42.2%Language:JavaScript 5.6%Language:SCSS 1.3%