An Angular wrapper for localstorage/sessionstorage access.
Feel free to take a look at the DEMO / API.
Install via npm:
npm install ngx-localstorage
If you want to use the ngxLocalStorage
directive you have to import it either in yopur module or component/directive.
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 {}
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
- 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:
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.
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 { }
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.
Thanks goes to these wonderful people (emoji key):
Basti Hoffmann 💻 📖 🤔 🚧 📦 👀 |
Matt Lewis 🚧 |
Mark 🐛 💡 |
NewteqDeveloper 💡 |
Abhishek Datta 🚧 |
Mikael Syska 🐛 |
Vladimir Stempel 💻 |
assureclaims 💻 🎨 📖 |
Boris-Bodin 🚧 |
This project follows the all-contributors specification. Contributions of any kind welcome!