Introduction
This article shows how to create a To Do List using AngularJS.
You can create many types of applications using AngularJS, here I show how to create another useful and easy to create application named "To Do List".
Using this application you can create a list of work that needs to be completed and can specify those works that are completed. This application will also show you how much work is still to be done.
Step 1
First of all you need to add an external Angular.js file to your application, "angular.min.js".
For this you can go to the AngularJS official site or can download my source code and then fetch it or you can click on this link to download it: ANGULARJS.
After downloading the external file you need to add this file to the Head section of your application.
<head runat="server">
<title></title>
<script src="angular.min.js"></script>
</head>
Step 2
Now after adding the External JS file the first thing you need to do is to add ng-app in the <HTML> Tag otherwise your application will not run.
<html ng-app xmlns="http://www.w3.org/1999/xhtml">
Now I will work on the View/JavaScript of this application.
Add this code to the head section:
<script>
function x($scope) {
$scope.schedule = [
{ work: 'Wash the Car', completed: true },
{ work: 'Meet Ajay', completed: false }];
$scope.addNew = function () {
$scope.schedule.push({ work: $scope.todoWork, completed: false });
$scope.todoWork = '';
};
$scope.pending = function () {
var count = 0;
angular.forEach($scope.schedule, function (todo) {
count += todo.completed ? 0 : 1;
});
return count;
};
}
</script>
Here I created three functions, the first is the main function to hold the two other functions and some initial values as well.
The second function is for adding new work to the To Do List.
The third function finds the pending work and counts how much is completed.
First I created a function named "x", in this function some initial values are declared under the variable "schedule".
After this a new function is created and named "addNew", this function will help to enter new work to the list.
At the end another function is created, "pending()"; this function will count the number of pending and total work and will show how much work is still pending of all the work.
Now our work on the View is completed and we can move to the ViewModel
Step 3
Write this code in the body section:
<div ng-app>
<h2>To Do List</h2>
<div ng-controller="x">
<span>{{pending()}} of {{schedule.length}} pending</span>
<ul>
<li ng-repeat="todo in schedule">
<input type="checkbox" ng-model="todo.completed">
<span class="completed-{{todo.completed}}">{{todo.work}}</span>
</li>
</ul>
<input type="text" ng-model="todoWork" size="30" placeholder="add new work here">
<input class="btn-primary" type="button" ng-click="addNew()" value="add">
</div>
</div>




Join the conversation! Your thoughts help the community grow.