Skip to content

Instantly share code, notes, and snippets.

@nuweb
Created June 19, 2015 01:28
Show Gist options
  • Save nuweb/0d16779a3e6b242145d0 to your computer and use it in GitHub Desktop.
Save nuweb/0d16779a3e6b242145d0 to your computer and use it in GitHub Desktop.
AngularJS Todo App
angular.module("myApp", [])
.controller("TodoCtrl", ["$scope", function($scope){
// clear input todo text
$scope.lastTodoClass = "odd";
$scope.todoText = "";
// default todos
$scope.todos = [
{text:'Learn AngularJS', done:false, class:"even"},
{text: 'Build an app', done:false, class:"odd"}
];
// get total todos
$scope.getTotalTodos = function () {
return $scope.todos.length;
};
// add a todo
$scope.addTodo = function () {
var todoClass = $scope.lastTodoClass==="odd" ? "even" : "odd";
$scope.lastTodoClass = todoClass;
$scope.todos.push({text:$scope.todoText, done:false, class: todoClass});
$scope.todoText = '';
};
// clear completed todos
$scope.clearCompleted = function () {
$scope.todos = $scope.todos.filter(function(todo) {
return !todo.done;
});
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment