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 --save
Usage
If you want to use the ngxLocalStorage
directive you have to import the module.
NgxLocalstorageDirectiveModule
Import 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
- Type:
- allowNull
- Type:
boolean?
- Determines if null | 'null' values should be stored.
- Default: true
- Type:
- storageType
- Type:
StorageType?
- Determines the storage type.
- Default: 'localStorage'
- Type:
- delimiter
- Type:
string?
- Determines the delimiter in between prefix and key.
- Default: underscore('_')
- Type:
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!