TO DO LIST (Index.html)
<!doctype html> <html ng-app="todoApp"> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script> <script src="todo.js"></script> <style> .done-true { text-decoration: line-through; color: grey; } </style> </head> <body> <h2>Todo</h2> <div ng-controller="TodoListController as todoList"> <span>{{remaining()}} of {{todos.length}} remaining</span> [ <a href="" ng-click="archive()">archive</a> ] <ul class="unstyled"> <li ng-repeat="todo in todos"> <label class="checkbox"> <input type="checkbox" ng-model="todo.done"> <span class="done-{{todo.done}}">{{todo.text}}</span> </label> </li> </ul> <form ng-submit="addTodo()"> <input type="text" ng-model="todoText" size="30" placeholder="add new todo here"> <input class="btn-primary" type="submit" value="add"> </form> </div> </body> </html>
Todo.js
angular.module('todoApp', []) .controller('TodoListController', function() { todos = [ {text:'learn AngularJS', done:true}, {text:'build an AngularJS app', done:false}]; addTodo = function() { todos.push({text:todoText, done:false}); todoText = ''; }; remaining = function() { var count = 0; angular.forEach(todos, function(todo) { count += todo.done ? 0 : 1; }); return count; }; archive = function() { var oldTodos = todos; todos = []; angular.forEach(oldTodos, function(todo) { if (!todo.done).todos.push(todo); }); }; });