AngularJS Todo List App

Getting Started

I've used CDN for Bootstrap and Angular JS so you need internet connection for them to work.

index.html

This is our index where we show our todo app list.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="app">
  3. <meta charset="utf-8">
  4. <title>AngularJS Todo List App</title>
  5. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  7. <style type="text/css">
  8. .div-list{
  9. background-color: #4CAF50;
  10. border: 1px solid #FFFFFF;
  11. color: #FFFFFF;
  12. padding: 10px 24px;
  13. cursor: pointer;
  14. width: 100%;
  15. display: block;
  16. margin-top:1px;
  17. }
  18. .div-list:not(:last-child) {
  19. border-bottom: none;
  20. }
  21. .div-list:hover {
  22. background-color: #FFFFFF;
  23. color: #4CAF50;
  24. border: 1px solid #4CAF50;
  25. }
  26. .mytask{
  27. margin-top:10px;
  28. }
  29. </style>
  30. </head>
  31. <body ng-controller="myController">
  32. <div class="container">
  33. <h1 class="page-header text-center">AngularJS Todo List App</h1>
  34. <div class="row">
  35. <div class="col-sm-4 col-sm-offset-4">
  36. <h3>Total Task : <b>{{ countAllTask() }}</b></h3>
  37. {{ counter }}
  38. <hr>
  39. <h3>Todo Task : <b>{{ (lists | filter : { complete: 'false' } : true).length }}</b></h3>
  40. <form ng-submit="addTask()">
  41. <input type="text" class="form-control" placeholder="New Task" ng-model="newtask">
  42. </form>
  43. <div class="mytask">
  44. <div class="div-list" ng-repeat="list in lists | filter:{ complete: 'false' }">
  45. <button type="button" class="close" ng-click="deleteTask(list)">&times;</button>
  46. <input type="checkbox" ng-model="list.Selected" ng-change="completeTask(list)"> {{ list.name }}
  47. </div>
  48. </div>
  49. <hr>
  50. <h3>Completed Task : {{ (lists | filter : { complete: 'true' } : true).length }}</h3>
  51. <div class="mytask">
  52. <div class="div-list" ng-repeat="list in lists | filter:{ complete: 'true' }">
  53. <button type="button" class="close" ng-click="deleteTask(list)">&times;</button>
  54. {{ list.name }}
  55. </div>
  56. </div>
  57. <button type="button" class="btn btn-danger mytask" ng-click="deleteCompleted()">Delete All Completed</button>
  58. <br><br>
  59. </div>
  60. </div>
  61. </div>
  62. <script src="angular.js"></script>
  63. </body>
  64. </html>

angular.js

And this is our angular js script.
  1. var app = angular.module('app', []);
  2. app.controller('myController', function($scope, $filter){
  3. $scope.lists = [
  4. {
  5. taskid: 1,
  6. name: 'Learn Angular',
  7. complete: 'true'
  8. },
  9. {
  10. taskid: 2,
  11. name: 'Create Angular Apps',
  12. complete: 'false'
  13. },
  14. {
  15. taskid: 3,
  16. name: 'Submit Apps',
  17. complete: 'false'
  18. }
  19. ]
  20.  
  21. $scope.countAllTask = function(){
  22. return $scope.lists.length;
  23. }
  24.  
  25. $scope.addTask = function(){
  26. var obj = $scope.lists.length-1;
  27.  
  28. if(obj < 0){
  29. var lastid = 1;
  30. }
  31. else{
  32. var lastid = $scope.lists[obj].taskid;
  33. }
  34.  
  35. $scope.lists.push({
  36. taskid: lastid + 1,
  37. name: $scope.newtask,
  38. complete: 'false'
  39. });
  40.  
  41. $scope.newtask = '';
  42. }
  43.  
  44. $scope.deleteTask = function(list){
  45. var index = $scope.lists.indexOf(list);
  46. $scope.lists.splice(index,1);
  47. }
  48.  
  49. $scope.completeTask = function(list){
  50. $scope.lists.find(function(v) {
  51. return v.taskid == list.taskid;
  52. }).complete = 'true';
  53. }
  54.  
  55. $scope.deleteCompleted = function(){
  56. var completed = $filter('filter')($scope.lists, { complete: 'true' });
  57. angular.forEach(completed, function(list){
  58. var index = $scope.lists.indexOf(list);
  59. $scope.lists.splice(index,1);
  60. });
  61. }
  62.  
  63. });
That end this tutorial. Happy Coding :)

Add new comment