Introduction

In this article, we will create a simple Todo application in angular using local storage. This app will give functionality like adding Tasks, Mark Task Complete, and Deleting single or All tasks from the list.

Simple Todo App in Angular With Local Storage

What Is Local Storage?

Local Storage is a data storage type of web storage. This allows the JavaScript sites and apps to store and access the data without expiration. This means that the data will always be persisted and will not expire. So, data stored in the browser will be available even after closing the browser window. In short, the localStorage holds the data with no expiry date, which is available to the user even after closing the browser window.

Local Storage Methods

Create Angular Application

Create a new angular application by the following command.

ng new <AppName>

Simple Todo App in Angular With Local Storage

For this todo application, I will use ngModel to pass data between the HTML file and the ts file. To use this directive, we must import FormsModule in the import array of the app module.

import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Here I'm using bootstrap for designing. Please refer to this article if you want to know how to add bootstrap in Angular.

What We Are Going to Do

As seen in the image below, data will be stored in this format in local storage.

Simple Todo App in Angular With Local Storage

Save Function

Save() {
   localStorage.setItem("todo", JSON.stringify(this.list));
}

Get Function

GetAll() {
  let value = localStorage.getItem("todo");
  if (value != '' && value != null && typeof value != "undefined") {
    this.list = JSON.parse(value!);
  }
}

Add Function


  Add() {
    let obj = {
      TaskName: this.task,
      IsComplete: false
    };
    this.list.push(obj);
    this.Save();
    this.task = '';
  }

Delete and Delete All Function

Delete(index: number) {
  if (this.list.length > index) {
    this.list.splice(index, 1);
    this.Save();
  }
}

DeleteAll() {
  this.list = [];
  this.Save();
}

Change Status Function

ChangeStatus(index: number, currentValue: boolean) {
  if (this.list.length > index) {
    let obj = this.list[index];
    if (obj != null && typeof obj != "undefined") {
      obj.IsComplete = !currentValue;
      this.list[index] = obj;
      this.Save();
    }
  }
}

Design Part

app.component.html

<div class="row m-3">
  <div class="col-6">
    <input type="text" class="form-control" placeholder="Enter Task" [(ngModel)]="task" />
  </div>
  <div class="col-1">
    <button class="btn btn-primary" (click)="Add()">Add</button>
  </div>
  <div class="col-2">
    <button class="btn btn-danger" (click)="DeleteAll()">Delete All</button>
  </div>
</div>

<div class="row m-3" *ngFor="let item of list; let i=index">
  <div class="col-1">
    <button class="btn btn-danger btn-sm pull-right" (click)="Delete(i)">Delete</button>
  </div>
  <div class="col-5">
    <div class="form-check my-1">
      <input class="form-check-input" (change)="ChangeStatus(i,item.IsComplete)" type="checkbox"
        [checked]="item.IsComplete" id="{{i}}">
      <label class="form-check-label" for="{{i}}" *ngIf="item.IsComplete">
        <s>{{item.TaskName}}</s>
      </label>
      <label class="form-check-label" for="{{i}}" *ngIf="!item.IsComplete">
        {{item.TaskName}}
      </label>
    </div>
  </div>
</div>

app.component.ts

import { Component, OnInit } from '@angular/core';

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

  list: any = []
  task: string = "";

  ngOnInit(): void {
    this.GetAll();
  }

  Add() {
    let obj = {
      TaskName: this.task,
      IsComplete: false
    };
    this.list.push(obj);
    this.Save();
    this.task = '';
  }

  ChangeStatus(index: number, currentValue: boolean) {
    if (this.list.length > index) {
      let obj = this.list[index];
      if (obj != null && typeof obj != "undefined") {
        obj.IsComplete = !currentValue;
        this.list[index] = obj;
        this.Save();
      }
    }
  }

  Delete(index: number) {
    if (this.list.length > index) {
      this.list.splice(index, 1);
      this.Save();
    }
  }

  DeleteAll() {
    this.list = [];
    this.Save();
  }

  Save() {
    localStorage.setItem("todo", JSON.stringify(this.list));
  }

  GetAll() {
    let value = localStorage.getItem("todo");
    if (value != '' && value != null && typeof value != "undefined") {
      this.list = JSON.parse(value!);
    }
  }
}

Add Task

Simple Todo App in Angular With Local Storage

Change Status of Task

Simple Todo App in Angular With Local Storage

Delete Task

Simple Todo App in Angular With Local Storage

You can download the source code of this project from my GitHub.