2015-12-14 20:31:40 -08:00
|
|
|
// #docregion
|
2016-05-03 14:06:32 +02:00
|
|
|
import { Component } from '@angular/core';
|
|
|
|
|
|
|
|
import { Todo } from './todo';
|
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-09-01 02:08:57 +01:00
|
|
|
styles: ['a { cursor: pointer; cursor: hand; }']
|
2015-12-14 20:31:40 -08:00
|
|
|
})
|
2016-06-13 00:41:33 +02:00
|
|
|
export class TodoAppComponent {
|
2015-12-14 20:31:40 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|