Creating a Todo List Application using Angular JS Tutorial

In this tutorial, you will learn how to Create a Todo List using Angular JS. The tutorial aims to provide IT/CS Students and new programmers with a reference to learn with for developing a simple web application using Angular JS and LocalStorage. Here, a step-by-step tutorial with snippets of building a simple Todo List App is provided with a source code zip file that is free to download.

Todo List App - Angular JS

About the Todo List Application

For this tutorial, we will be creating a very simple Todo List Application that contains the relevant features and functionalities that are commonly found in said application. We will be using Angular JS as the JavaScript Framework for building the application. As for the database, we will only LocalStorage web API.

Features and Functionalities

Here are the different Features and Functionalities of the Todo list application that we are building.

  • Add New Todo Item
  • New Item Insertion Success Message
  • Todo Item List
  • Checkbox for Done/Undone Todo Items
  • Todo Item Strike Through Text if already Done
  • Remove Todo Item from the List
  • Confirmation before removing Todo Item

Creating the Interface

The below snippets is an HTML file that contains the elements scripts for the form, to-do list, and alert messages. It also contains the AngularJS attributes that we need for initializing the application and functionalities. Save the file as index.html. The snippet below loads the external libraries or plugins using CDN, an internet connection is needed to run the application properly.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Angular JS Todo List App</title>
  7. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
  8. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
  9. <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js" integrity="sha512-naukR7I+Nk6gp7p5TMA4ycgfxaZBJ7MO5iC3Fp6ySQyKFHOGfpkSZkYVWV5R7u7cfAicxanwYQ5D1e17EfJcMA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  10. <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.8.3/angular.js" integrity="sha512-klc+qN5PPscoGxSzFpetVsCr9sryi2e2vHwZKq43FdFyhSAa7vAqog/Ifl8tzg/8mBZiG2MAKhyjH5oPJp65EA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  11. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
  12. html, body{
  13. min-height:100%;
  14. width:100%;
  15. }
  16. #todoList .list-group-item .form-check , #todoList .list-group-item .form-check *{
  17. cursor: pointer;
  18. }
  19. #todoList .list-group-item[item-checked="1"] .form-check>label{
  20. text-decoration: line-through;
  21. }
  22. </style>
  23. </head>
  24. <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient">
  25. <div class="container">
  26. <a class="navbar-brand" href="./">Angular JS Todo List App</a>
  27. <div>
  28. <a href="https://sourcecodester.com" class="text-light fw-bolder h6 text-decoration-none" target="_blank">SourceCodester</a>
  29. </div>
  30. </div>
  31. </nav>
  32. <div class="container-fluid px-5 my-3" ng-app="SampleApp" ng-controller="FormController">
  33. <div class="col-lg-6 col-md-8 col-sm-12 mx-auto">
  34. <div class="card rounded-0 shadow mb-3">
  35. <div class="card-body">
  36. <div class="container-fluid">
  37. <form ng-submit="formSubmit()">
  38. <div class="alert alert-success mb-3" ng-show="success_msg">{{success_msg}}</div>
  39. <div class="alert alert-danger mb-3" ng-show="error_msg">{{error_msg}}</div>
  40. <div id="item-list">
  41. <div class="d-flex w-100 mb-3 justify-content-between align-items-end input-item">
  42. <div class="col-auto flex-shrink-1 flex-grow-1 pe-4">
  43. <label class="form-label">Todo</label>
  44. <input type="text" class="form-control form-control-sm rounded-0" ng-model="todo" name="todo" required>
  45. </div>
  46. </div>
  47. </div>
  48. <div class="mb-3 d-grid gap-2">
  49. <button type="submit" class="btn btn-primary btn-block rounded-pill"><i class="fa-solid fa-save"></i> Add to List</button>
  50. </div>
  51. </form>
  52. </div>
  53. </div>
  54. </div>
  55. <div class="card rounded-0 shadow">
  56. <div class="card-header">
  57. <div class="card-title"><b>Todo List</b></div>
  58. </div>
  59. <div class="card-body">
  60. <div class="container-fluid">
  61. <div id="todoList" class="list-group">
  62. <div class="list-group-item" item-checked = "{{item.is_done}}" ng-repeat="item in items track by $index">
  63. <div class="d-flex justify-content-between align-items-center">
  64. <div class="col-auto flex-shrink-1 flex-grow-1">
  65. <div class="form-check">
  66. <input class="form-check-input" type="checkbox" item-id="{{item.id}}" value="" id="todo{{item.id}}" ng-click="event = $event" ng-true-value="1" ng-false-value="0" ng-change="todoCheckChange(event)" ng-model="item.is_done" ng-checked="item.is_done === 1">
  67. <label class="form-check-label" for="todo{{item.id}}">
  68. {{item.todo}}
  69. </label>
  70. </div>
  71. </div>
  72. <div class="col-auto">
  73. <button class="btn btn-sm rounded-circle border btn-outline-danger" ng-click="removeTodo(item.id)"><i class="fa fa-trash"></i></button>
  74. </div>
  75. </div>
  76.  
  77.  
  78. </div>
  79. </div>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. <script src="app.js"></script>
  86. </body>
  87. </html>

Creating the Main Script

The snippet below is a JavaScript file that contains the script for initializing the application using AngularJS. It also contains the functions for generating a unique ID for the Todo items. Overall it has 3 different methods or functions in Angular controller. It has the Form Submission, Checkboxes, and Remove Button functions. Save the file as app.js.

  1. var mymodule = angular.module("SampleApp",[]);
  2. var items = JSON.parse(localStorage.getItem('todo-list')) || [];
  3.  
  4. function generate_id(){
  5. var id;
  6. while(true){
  7. id = Math.ceil(Math.random() * (9999 - 1) + 1);
  8. var key = Object.values(items).findIndex( item => item.id == id )
  9. if(key < 0)
  10. break;
  11. }
  12. return id;
  13. }
  14.  
  15. /**
  16.   * Angular Controller
  17.   */
  18.  
  19. mymodule.controller("FormController", function ($scope, $http){
  20. $scope.items = items;
  21. $scope.todo=''
  22. $scope.success_msg = "";
  23.  
  24. /**
  25.   *
  26.   * Form Submit
  27.   * Adding new todo item
  28.   *
  29.   */
  30.  
  31. $scope.formSubmit = function(e){
  32. $scope.success_msg = "";
  33. var id = generate_id()
  34. items.push({id:id, todo: $scope.todo, is_done:false})
  35. localStorage.setItem('todo-list', JSON.stringify(items))
  36. $scope.items = items
  37. $scope.success_msg = "New todo item has been added successfully";
  38. $scope.todo=''
  39. }
  40.  
  41. /**
  42.   * Event onChange Listener for Todo checkboxes
  43.   * @param {e} event
  44.   */
  45.  
  46. $scope.todoCheckChange =function(e){
  47. $scope.success_msg = "";
  48. var is_checked = e.target.checked ? 1 : 0
  49. // console.log(is_checked)
  50. var id = e.target.getAttribute('item-id')
  51. var key = Object.values(items).findIndex( item => item.id == id )
  52. items[key]['is_done'] = is_checked
  53. localStorage.setItem('todo-list', JSON.stringify(items))
  54. $scope.items = items
  55. $scope.list_success_msg = "Item has been updated";
  56.  
  57. }
  58.  
  59. /**
  60.   * Function for removing the todo item from list w/ confirmation
  61.   * @param {*} id
  62.   *
  63.   */
  64. $scope.removeTodo = function (id){
  65. $scope.success_msg = "";
  66. if(confirm(`Are you sure to remove the selected todo item?`) === false)
  67. return false;
  68. var key = Object.values(items).findIndex( item => item.id == id )
  69. delete items[key];
  70. items= Object.values(items).filter( item => item != null )
  71. localStorage.setItem('todo-list', JSON.stringify(items))
  72. $scope.items = items
  73. }
  74. })

That's it! Check the Todo List Item in your local storage. Open your browser's Developer Tools and navigate the panel to the Application Panel of the Devtools. Then on the right menu bar of the Application Panel, click the Local Storage menu and select the localhost site or your current site. See the image below.

Local Web API - DevTools

There you go! You can now test the Todo List App in AngularJS on your end and see if the features and functionalities that I have mentioned exist and works properly. I have provided the complete source code zip file. You can download it by clicking the Download Button located below this article.

That's the end of this tutorial. I hope this Todo List App in AngularJS Tutorial helps you with what you are looking for and that you'll find something useful from the source code for your current and future projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding :)

Add new comment