Skip to content

Commit

Permalink
CREATE: new todo elements
Browse files Browse the repository at this point in the history
  • Loading branch information
amejiarosario committed Oct 17, 2016
1 parent 2712e68 commit 049b52b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/app/todo/todo.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

<header class="header">
<h1>Todo</h1>
<input class="new-todo" placeholder="What needs to be done?" autofocus>
<input class="new-todo"
placeholder="What needs to be done?"
[(ngModel)]="newTodo"
(keyup.enter)="addTodo()"
autofocus>
</header>

<!-- This section should be hidden by default and shown when there are todos -->
Expand Down
10 changes: 9 additions & 1 deletion src/app/todo/todo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,13 @@ export class TodoComponent implements OnInit {
this.todos = todos;
this.activeTasks = this.todos.filter(todo => todo.isDone).length;
});
}
}

addTodo(){
this.todoService.add({ title: this.newTodo, isDone: false }).then(() => {
return this.getTodos();
}).then(() => {
this.newTodo = ''; // clear input form value
});
}
}
7 changes: 7 additions & 0 deletions src/app/todo/todo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ export class TodoService {
return new Promise(resolve => resolve(todos));
}

add(data) {
return new Promise(resolve => {
todos.push(data);
resolve(data);
});
}

}

0 comments on commit 049b52b

Please sign in to comment.