Local storage is a web storage feature in modern browsers that allows storing data persistently in a key-value format. Angular applications use local storage to retain user-specific data even after page reloads or session ends.
Angular does not provide a built-in local storage service. However, you can use the window.localStorage API or third-party libraries like ngx-webstorage or angular-web-storage.
Storing Data
localStorage.setItem('username', 'JohnDoe');
Retrieving Data
const username = localStorage.getItem('username');
console.log(username); // Output: JohnDoe
Removing Data
localStorage.removeItem('username');
Clearing All Data
localStorage.clear();
It’s a good practice to create a reusable service for local storage operations.
local-storage.service.ts
export class LocalStorageService {
private secretKey = 'your-secret-key';
setItem(key: string, value: any): void {
localStorage.setItem(key, JSON.stringify(value));
}
getItem(key: string): any {
const data = localStorage.getItem(key);
return data ? JSON.parse(data) : null;
}
setEncryptedItem(key: string, value: any): void {
const encryptedValue = CryptoJS.AES.encrypt(JSON.stringify(value), this.secretKey).toString();
localStorage.setItem(key, encryptedValue);
}
getDecryptedItem(key: string): any {
const encryptedData = localStorage.getItem(key);
if (!encryptedData) return null;
const bytes = CryptoJS.AES.decrypt(encryptedData, this.secretKey);
return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
}
removeItem(key: string): void {
localStorage.removeItem(key);
}
clear(): void {
localStorage.clear();
}
}
import { Component } from '@angular/core';
import { LocalStorageService } from './local-storage.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent {
constructor(private localStorageService: LocalStorageService) {
this.localStorageService.setItem('username', 'JohnDoe');
console.log(this.localStorageService.getItem('username'));
this.localStorageService.setEncryptedItem('secureData', 'Sensitive Info');
console.log(this.localStorageService.getDecryptedItem('secureData'));
}
}
Install using:
npm install ngx-webstorage
Modify app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { NgxWebstorageModule } from 'ngx-webstorage';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, NgxWebstorageModule.forRoot()],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
import { Component } from '@angular/core';
import { LocalStorageService } from 'ngx-webstorage';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent {
constructor(private localStorage: LocalStorageService) {
this.localStorage.store('username', 'JohnDoe');
console.log(this.localStorage.retrieve('username'));
}
}
Local storage is a powerful and simple tool for storing small amounts of data in an Angular application. For better maintainability, use a service approach or third-party libraries for more advanced functionality.
Work with our skilled Angular developers to accelerate your project and boost its performance.
Hire Angular Developers