2015-12-14 20:31:40 -08:00
|
|
|
// #docregion
|
2016-04-27 11:28:22 -07:00
|
|
|
import {Component} from '@angular/core';
|
2016-04-06 09:10:52 +03:00
|
|
|
import {Todo} from './todo';
|
|
|
|
|
import {TodoList} from './todo_list';
|
|
|
|
|
import {TodoForm} from './todo_form';
|
2015-12-14 20:31:40 -08:00
|
|
|
|
|
|
|
|
@Component({
|
2015-12-16 15:39:00 -08:00
|
|
|
selector: 'todo-app',
|
2015-12-14 20:31:40 -08:00
|
|
|
template: `
|
|
|
|
|
<h2>Todo</h2>
|
|
|
|
|
<span>{{remaining}} of {{todos.length}} remaining</span>
|
2016-01-22 02:40:37 -08:00
|
|
|
[ <a (click)="archive()">archive</a> ]
|
2015-12-14 20:31:40 -08:00
|
|
|
|
|
|
|
|
<todo-list [todos]="todos"></todo-list>
|
|
|
|
|
<todo-form (newTask)="addTask($event)"></todo-form>`,
|
2016-01-22 02:40:37 -08:00
|
|
|
styles:['a { cursor: pointer; cursor: hand; }'],
|
2015-12-14 20:31:40 -08:00
|
|
|
directives: [TodoList, TodoForm]
|
|
|
|
|
})
|
|
|
|
|
export class TodoApp {
|
|
|
|
|
todos: Todo[] = [
|
2016-04-06 09:10:52 +03:00
|
|
|
{text: 'learn angular', done: true},
|
|
|
|
|
{text: 'build an angular app', done: false}
|
2015-12-14 20:31:40 -08:00
|
|
|
];
|
|
|
|
|
|
2015-12-15 16:09:09 +05:30
|
|
|
get remaining() {
|
2016-04-06 09:10:52 +03:00
|
|
|
return this.todos.filter(todo => !todo.done).length;
|
2015-12-14 20:31:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
archive(): void {
|
2016-04-06 09:10:52 +03:00
|
|
|
let oldTodos = this.todos;
|
2015-12-14 20:31:40 -08:00
|
|
|
this.todos = [];
|
2016-04-06 09:10:52 +03:00
|
|
|
oldTodos.forEach(todo => {
|
|
|
|
|
if (!todo.done) { this.todos.push(todo); }
|
2015-12-14 20:31:40 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addTask(task: Todo) {
|
|
|
|
|
this.todos.push(task);
|
|
|
|
|
}
|
|
|
|
|
}
|