Adding a TodoService class

Now, let's create a TodoService class that acts as a service repository to manage todo items. The following is the code snippet of the todo.service.ts file:

import { Todo } from './todo'    
export class TodoService {
todos: Array<Todo>
constructor() {
this.todos = [new Todo('First item'),
new Todo('Second item'),
new Todo('Third item')];
}
getPending() {
return this.todos.filter((todo: Todo) => todo.completed ===
false);
}
getCompleted() {
return this.todos.filter((todo: Todo) => todo.completed ===
true);
}
remove(todo: Todo) {
this.todos.splice(this.todos.indexOf(todo), 1);
}

add(title: string) {
this.todos.push(new Todo(title));
}
toggleCompletion(todo: Todo) {
todo.completed = !todo.completed;
}
removeCompleted() {
this.todos = this.getPending();
}
}

We have created the TodoService class with various methods to add, remove, and return the collection of todo items.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.18.220.243