Skip to content

imajinengine/ngx-localstorage

 
 

Repository files navigation

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.

Buy Me A Coffee

Installation

Install via npm:

npm install ngx-localstorage --save

Usage

If you want to use the ngxLocalStorage directive you have to import the module.

Import NgxLocalstorageDirectiveModule

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

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

Configuration

For configuration provide it using the NGX_LOCAL_STORAGE_CONFIG InjectionToken.

  • 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:

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

💻 📖 🤔 🚧 📦 👀

Matt Lewis

🚧

Mark

🐛 💡

NewteqDeveloper

💡

Abhishek Datta

🚧

Mikael Syska

🐛

Vladimir Stempel

💻

assureclaims

💻 🎨 📖

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

About

An Angular wrapper for localstorage/sessionstorage access.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 55.9%
  • HTML 37.8%
  • JavaScript 4.2%
  • SCSS 2.1%