Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
localStorage.setItem(key,value)

How to store data in local storage in angular ?

Aman Jain, March 28, 2022

While working in angular application we want to store data in local storage for example want to store user login data or some calculations. In this article i will show you to store objects, array, integer, string etc. in local storage.

We will going to use native API for storing the data and then fetch data again, localStorage provides a good way to store the data in local storage by which we can store data in string format.

Simple syntax for set and get local storage

localStorage.setItem("item_key","item_value");

and to get data back

localStorage.getItem("item_key");

Let’s understand it with example in angular

Step 1 : Create an angular app

First step is to setup an angular app using below command

ng new example-component

Step 2: Create a Component

Create a component so we can use insert the script tag in it dynamically.

ng g c test-storage
import { Component, OnInit, Input } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';

@Component({
  selector: 'app-test-storage',
  templateUrl: './test-storage.component.html',
  styleUrls: ['./test-storage.component.css']
})
export class TestStorageComponent implements OnInit {
   
  constructor() { }

  ngOnInit() {

  }

}

Step 3 : Add and get local storage

Now add the logic for set and get local storage

import { Component, OnInit, Input } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';

@Component({
  selector: 'app-test-storage',
  templateUrl: './test-storage.component.html',
  styleUrls: ['./test-storage.component.css']
})
export class TestStorageComponent implements OnInit {
   
  constructor() { }

  ngOnInit() {
     if(!this.getData("user")){
        this.setData("user",{id:1,name:"Loise"});
     }
  }

  saveData(key:string,value:string){
    localStorage.setItem(key,JSON.stringify(value));
  }
  getData(key:string):any{
   return JSON.parse(localStorage.getItem(key));
  }

}

Create local storage service for global use

Or we can create a separate service for storage so we can use it globally anywhere

so create a service using below command

ng generate service Storage

it will generate a class like below

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class StorageService {

  saveData(key:string,value:string){
    localStorage.setItem(key,JSON.stringify(value));
  }
  getData(key:string):any{
   return JSON.parse(localStorage.getItem(key));
  }
}

Here we added saveData method to store the data in local storage and also notice that we json stringify the value using JSON.strigify because using this we can store any kind of data, and same at the time of get in getData method we parsed the output.

How to store object in local storage?

As you can see we have used JSON.stringify and JSON.parse method before saving the local storage so now we can use any type of data type to store and then we can again parse them while outputting as below

saveData(key:string,value:string){
    localStorage.setItem(key,JSON.stringify(value));
  }
  getData(key:string):any{
   return JSON.parse(localStorage.getItem(key));
  }

Also Read : How to use ngFor and ngIf on same element in angular 12 ?

Related

Javascript Angular angularlocal storage

Post navigation

Previous post
Next post

Related Posts

How to submit multipart form data in angular reactive form ?

September 12, 2021October 31, 2021

In this tutorial, you will learn uploading a multipart reactive form in angular using HttpClient. In a webpage uploading files as multipart form data is a important aspect. If you are a new then you can read What is Angular and how to install angular ? So, for this tutorial…

Read More
Javascript Password and confirm password validation in angular

Password and confirm password validation in angular ?

September 11, 2021November 9, 2023

Angular provides built-in library for validation but it doesn’t have built in validation for Password and confirm password validation in angular. so in this tutorial i am going to explain how can we make custom validation for this same as angular built-in validations. Confirm password most useful utility when we…

Read More
Javascript Laravel Multi Select Tag Autocomplete Using Select2

Laravel Multi Select Tag Autocomplete Using Select2

July 19, 2022July 19, 2022

In this article we will learn to use Laravel Multi Select Tag Autocomplete Using Select2. Select2 is useful when we want live search of bulk data or to convert the existing select boz with multi features like search, multi select and options customizations. In this article we will cover multiple…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version