/todo.component.ts Secret
Created
September 1, 2023 13:06
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// updated | |
// src/app/components/todo/todo.component.ts | |
import { Component, OnInit } from '@angular/core'; | |
import { TodoService } from '../../services/todo.service'; | |
import { Signal } from '@angular/core'; // import Signal | |
import { TodoList } from '../../models/todo.model'; | |
@Component({ | |
selector: 'app-todo', | |
templateUrl: './todo.component.html', | |
styleUrls: ['./todo.component.css'] | |
}) | |
export class TodoComponent implements OnInit { | |
todoList$: Signal<TodoList>; // signal of todo list | |
constructor(private todoService: TodoService) {} | |
ngOnInit(): void { | |
this.todoList$ = this.todoService.getTodoList(); // get the todo list from the service | |
} | |
// toggle the completion status of a todo item | |
async toggleCompleted(id: number): Promise<void> { | |
// find the todo item by id | |
const todoItem = this.todoList$.value.find(todo => todo.id === id); // use value property to access the current value of the signal | |
// update the todo item on the service | |
await this.todoService.updateTodoItem(id, !todoItem.completed); // use async/await syntax to handle the asynchronous operation | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment