×

How to create a Todo List App in Angular 7

What is a Todo List App?

Todo List app is an app where users can add various tasks that they want to perform and have an idea of every task. If any task is done, then the user can easily remove the task from that list without affecting the order of other tasks.

The prerequisite for this app is just the basic knowledge of HTML and CSS. We will implement this app by the following steps:

  1. Create an empty text box where the user can enter his tasks.
  2. Add button which will add the task to the list.
  3. Display the list of tasks created by the user.
  4. Remove button for every task to remove if that work is completed.

Step1: Create the angular app by the following command in your desired folder:

ng new TodoListApp

Step2: Open that into the VS code or any other editor.

Step3: Just run the app on localhost 4200 by this command.

ng serve --open 

Or

 ng serve

Now, we will create the title of our app using the h1 tag, so we will edit the app.components.html file and write into that from scratch

<h1>{{title}}</h1>
<input type="text" placeholder="enter your next work" #task/>
<br>
<br>
<button (click)="addTask(task.value)">Add work</button>
<br>
<br>

Now, we will use the input tag of type text to enter our new tasks, and we will give it the id as ‘task’.

The br tag is used to give new line spaces as we are not focusing more on the CSS but only on the app.

We will add the button to add the task to the list, and we will attach a function named ‘addTask’, which will run when we click on that button. As parameters, we have passed the string of our input text.

Now, move to the app.component.ts part for implementing the functionality.

import { Component } from '@angular/core';
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'TodoApp';
 
}

You will get the above code by default when you create the app. Here, you can see that the title is the same as our app name, although we can change it.

Now, we will add the functions to the class name ‘AppComponent’.

We will require the list to store the tasks, and for each item on the list, we will have two values:

One is the id and the second one is the name where our main task will be written.

import { Component } from '@angular/core';
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'TodoApp';
  list:any[]=[];
  addTask(item:string){
    this.list.push({id:this.list.length,name:item});
    console.warn(this.list);
  }
 
 
}


Whenever we call this function, it will add that item to the list, and the id would be the size of the list.

Now in app.component.html, we will add the unordered list of tasks, and we will loop the list and print each item from the list into the unordered list.

Also, with each item on the list, we will add a button to remove the task and will call the function removeTask() when we click that button.

Task’s id will be passed as a parameter for the function.

This is the final app.component.html file

<h1>{{title}}</h1>
<input type="text" placeholder="enter your next work" #task/>
<br>
<br>
<button (click)="addTask(task.value)">Add work</button>
<br>
<br>
 
<ul *ngFor = 'let item of list'>
  <li>{{item.name}} <button (click)="removeTask(item.id)">Delete the task</button></li>
</ul>

Now in the app.component.ts, we will implement the removeTask() function.

Instead of removing the task, we will filter the list and update the list with the same task except that we want to remove.

So finally code will be:

import { Component } from '@angular/core';
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'TodoApp';
  list:any[]=[];
  addTask(item:string){
    this.list.push({id:this.list.length,name:item});
    console.warn(this.list);
  }
 
  removeTask(id:number){
    this.list=this.list.filter(item=>item.id!=id);
  }
}

For implementing the basic styling like font size and font family or background color, we will edit the style.css file in the environments folder.

Code:

/* You can add global styles to this file and also import other style files */
*{
    font-size: 25px;
}
body{
    background-color: cyan;
    font-size: 25px;
    margin-left:25%;
    font-family: Verdana, Geneva, Tahoma, sans-serif;
}
input{
    text-decoration: none;
   
}

So with just two files, app.component.html and app.component.ts, we created the to-do list app successfully.

Now, if you want more styling, then you can use bootstrap or add our own CSS files.

Final App: how-to-create-a-todo-list-app-in-angular-7

How to create a Todo List App in Angular 7

Related Topics

Advantages | Disadvantages of Angular 8

Advantages of Angular 8 There are some Advantages of angular 8 which are given below: It offers clean code developmentHigher PerformanceAn angular framework can take care of routing, which...

1 minute read.

Difference between Angular and React

Difference between Angular and React Parameters AngularReactType Angular is a complete framework. React is a JavaScript library, and much older compared...

2 minutes read.

Angular 8 Unit Testing

What is unit testing? Unit testing is a type of software testing where individual components of the software are tested. It is done during the development of any application. A unit may be...

6 minutes read.

Creating form in Angular 8

The angular reactive form is used to handle the user's input. We can use Angular form in the application to authorize the user to log in, to update profile, to enter information,...

2 minutes read.

Difference between Node.js and ASP.net

Introduction to Node.js V8 (the JavaScript engine operating inside Google Chrome) is packaged with certain libraries and is mostly used for input & output, which includes creating data and managing connection...

4 minutes read.

Event Binding in Angular 8

Event binding in Angular 8 Event binding is a data-binding from an element to a component. User actions such as keystrokes, mouse movement, clicks, and touches can be bound to the component property...

1 minute read.

Angular 8 NgStyle Directive

Angular 8 NgStyle Directive The ngStyle attribute is used to change and style the multiple properties of Angular. We can change the value, color, and size, etc. of the component. It is a built-in...

1 minute read.

Data Binding in Angular 8

What is Binding? Binding is the process which generates the connection between the application UI and the data which comes from the business logic. In Angular, it is called the automatic synchronization of...

3 minutes read.

Angular 8 Pipes

Angular 8 Pipes Pipes are a useful feature in Angular. These are the simple way to transform values in an Angular template. It takes the integers, strings, array, and dates as input separated with...

3 minutes read.

Angular CLI Commands

All CLI commands of Angular Angular CLI is a command-line interface which is used to initialize, develop, and maintain Angular applications. We can use these Command on command prompt or consequentially by an associated...

8 minutes read.

Dependency Injection in Angular 8

Dependency injection (DI), is an essential application design pattern. Angular 8 has its own DI framework, which used in the design of Angular application to increase efficiency and portability. Dependencies are the services that...

7 minutes read.

Angular 8 Libraries

Angular libraries are built a solution of general problems like presenting a unified user interface, presenting data, and allowing the data entry. Developers can create standard solutions for particular domains...

3 minutes read.

Two-way Data Binding in Angular 8

Two-way Data Binding in Angular 8 Two-way data binding is a synchronization between the model and the view. We can use the ngModel directive in two-way data binding. Custom two-way data binding is useful...

2 minutes read.

Angular 8 Error Fixing

We can get errors in Angular because of many causes. Let's see an example to see some specific types of errors. We have to create an app with the name "testing-app." In this app,...

4 minutes read.

Angular 8 Conclusion

Angular version 8 looks like a more accessible solution entirely focused on the modern technological trends added features. Like Ivy, route configurations use dynamic imports, builder APIs in the CLI, web worker support,...

2 minutes read.

Dynamic components in Angular 8

Dynamic components in Angular 8 The dynamic component is one of the versatile and core concept introduced in Angular, Component template is not fixed. An application needs to load new elements...

4 minutes read.

Angular 8 *ngFor Directive

Angular 8ngFor Directive The *ngFor directive is used to repeat to repeat a portion of HTML template once per each item from an iterable list (collection). The ngFor is an Angular...

2 minutes read.

$timeout service in AngularJS

The discipline of web development is expanding quickly. A technology that is released today will inevitably become obsolete in a few months. The webpages were static in the past and...

4 minutes read.

Angular 8 Forms

Angular forms are used to handle the user's input. We use Angular form in our application to authorize users to log in, to update profile, to enter information, and to perform many other...

3 minutes read.

ngSwitch Directive in Angular 8

Angular 8 ng-Switch Directive The ng-Switch Directive hides and shows the HTML elements depending on an expression. Child elements with the ng-switch-when directive will be displayed if it gets a match; otherwise, the component and...

2 minutes read.