Skip to main content

Creating a To-Do List Application Using AngularJS and jQuery

Screenshot
Body

In this tutorial we will create a To Do List Applicationr Using AngularJS. This code will list all your workflow when the user enters the textbox and submit. The code itself use AngularJS directives that can add list as an array by using dot notation push() and clear a list with the use of built-in dot notation filter() by just passing the item index key. The application uses the browser's local storage to store the data. This a user-friendly program feel free to modify and use in your system.

We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interpret those attributes as directives to bind inputted parts of the page to a model that represents a standard JavaScript variable.

Getting Started:

First, you have to download Bootstrap, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/ and jQuery for some other functions. Also, this is the link for the AngularJS https://angularjs.org/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it index.html.

  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3. <head>
  4. <title>To Do List</title>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  8. .finish{
  9. text-decoration: line-through;
  10. color:#ccc;
  11. }
  12. ul{
  13. list-style-type:none;
  14. }
  15. ul li{
  16. font-size:18px;
  17. }
  18. </style>
  19. </head>
  20. <body ng-controller="myController">
  21. <nav class="navbar navbar-default">
  22. <div class="container-fluid">
  23. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  24. </div>
  25. </nav>
  26. <div class="col-md-3"></div>
  27. <div class="col-md-6 well">
  28. <h3 class="text-primary">JavaScript - To Do List Application Using AngularJS</h3>
  29. <hr style="border-top:1px dotted #ccc;"/>
  30. <div class="col-md-4">
  31. <form name="form" ng-submit="addList()">
  32. <div class="form-group">
  33. <input class="form-control" type="text" placeholder="Enter list here..." ng-model="newlist" required="required" />
  34. </div>
  35. <center><button class="btn btn-primary" ng-disabled="form.$Invalid"><span class="glyphicon glyphicon-plus"></span> ADD</button></center>
  36. </form>
  37. </div>
  38. <div class="col-md-8">
  39. <button class="btn btn-danger pull-right" ng-click="clear()"><span class="glyphicon glyphicon-remove"></span> Clear List</button>
  40. <br /><br /><br />
  41. <ul>
  42. <li ng-repeat="list in lists">
  43. <input class="chk" type="checkbox" ng-model="list.finish" />
  44. <span ng-class="{finish:list.finish}">{{list.title}}</span>
  45. </li>
  46. </ul>
  47. </div>
  48. </div>
  49. <script src="js/angular.js"></script>
  50. <script src="js/script.js"></script>
  51. </body>
  52. </html>

Creating the Main Function

This code contains the main function of the application. This code will add some list when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory.

  1. var todos = [];
  2. var app=angular.module('myModule', [])
  3. .controller('myController', ['$scope', function($scope){
  4. if(typeof(Storage) !== "undefined") {
  5. if (!localStorage.todos) {
  6. todos = [
  7. {title: 'Creating A Interface', finish: false},
  8. {title: 'Creating A Database Model', finish: false},
  9. ]
  10. localStorage.todos = JSON.stringify(todos);
  11. }else{
  12. todos = JSON.parse(localStorage.todos)
  13. }
  14. }
  15. $scope.lists = todos;
  16.  
  17. $scope.addList = function(){
  18. $scope.lists.push({title: $scope.newlist, finish: false})
  19. if(typeof(Storage) !== "undefined") {
  20. if (localStorage.todos) {
  21. var arr = []
  22. var data = JSON.parse(localStorage.todos)
  23. Object.keys(data).map(k=>{
  24. arr.push(data[k])
  25. })
  26. arr.push({title: $scope.newlist, finish: false})
  27. localStorage.todos = JSON.stringify(arr);
  28. }else{
  29. localStorage.todos = JSON.stringify([{title: $scope.newlist, finish: false}])
  30. }
  31. }
  32. $scope.newlist = "";
  33.  
  34. $('.chk').each(function(r){
  35. $(this).change(function(){
  36. if(typeof(Storage) !== "undefined") {
  37. if (localStorage.todos) {
  38. var arr = []
  39. var data = JSON.parse(localStorage.todos)
  40. data[r].finish = $(this).prop('checked');
  41. localStorage.todos = JSON.stringify(data);
  42. }
  43. }
  44. })
  45. })
  46.  
  47. }
  48.  
  49. $scope.clear = function(){
  50. $scope.lists = $scope.lists.filter(function(item){
  51. return !item.finish;
  52. });
  53. localStorage.todos = JSON.stringify($scope.lists)
  54. }
  55. }])
  56. $(document).ready(function(){
  57. $('.chk').each(function(r){
  58. $(this).change(function(){
  59. if(typeof(Storage) !== "undefined") {
  60. if (localStorage.todos) {
  61. var arr = []
  62. var data = JSON.parse(localStorage.todos)
  63. data[r].finish = $(this).prop('checked');
  64. localStorage.todos = JSON.stringify(data);
  65. }
  66. }
  67. })
  68. })
  69. })

Demo

There you have it we successfully created a To Do List Application using AngularJS. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!!

Add new comment