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

Javascript Angular Click Event on Input Example

Angular Click Event on Input Example

September 22, 2022March 16, 2024

Angular Click Event on Input is an Angular event binding it triggers when user click or tap any DOM element and it fires a event of (click). Input events in angular can be handled using input and output decorators and in this tutorial we will learn Angular Click Event on…

Read More
Javascript Angular Dynamic Style CSS with Example

Angular Dynamic Style CSS with Example

October 5, 2022March 16, 2024

In this blog post, we’ll explore Angular dynamic style CSS with example in component’s styles based on certain conditions. Sometimes in our application we want dynamically change the color, size, font, background images so Angular provides various ways to conditionally apply styles to elements. In this post, we’ll explore three…

Read More
Javascript how to install angular

What is Angular and how to install angular ?

August 29, 2021October 1, 2021

Angular is a framework of javascript. It’s used for creating efficient and single page application with ease. We can define the angular as follow: Angular is a component based component framework. Angular is collection of libraries which include routing, forms, validations, http requests etc. Angular is easy to use and…

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

  • August 2025
  • July 2025
  • 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 Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version