Angular service for persisting key-value data.
Delegates to the IStorageStrategy provided via STORAGE_STRATEGY. Defaults to LocalStorageStrategy (i.e. localStorage).
localStorage
Inject this service instead of calling localStorage directly so that storage behaviour can be swapped application-wide through DI.
export class MyComponent { private readonly storage = inject(StorageService); public save(key: string, data: unknown): void { this.storage.setItem(key, JSON.stringify(data)); } public load(key: string): unknown | null { const raw = this.storage.getItem(key); return raw ? JSON.parse(raw) : null; }} Copy
export class MyComponent { private readonly storage = inject(StorageService); public save(key: string, data: unknown): void { this.storage.setItem(key, JSON.stringify(data)); } public load(key: string): unknown | null { const raw = this.storage.getItem(key); return raw ? JSON.parse(raw) : null; }}
Retrieve a value by key. Returns null when the key does not exist.
null
Remove the value for the given key.
Store a value under the given key.
Angular service for persisting key-value data.
Delegates to the IStorageStrategy provided via STORAGE_STRATEGY. Defaults to LocalStorageStrategy (i.e.
localStorage).Inject this service instead of calling
localStoragedirectly so that storage behaviour can be swapped application-wide through DI.Example