JsonPipe
to display a list of todo items in JSON format:[object Object],[object Object],[object Object]
JsonPipe
is used to transform objects and arrays into a JSON string.
This is a convenient way to display the entire structure of the todos
array directly in the template without the need to use console.log
and see how data will change when you add, remove or update todos.@for
block is used to iterates over the todos
signal, using the id
property for tracking each todo item.ngClass
directive applies the line-through
class to the <span>
element if the item completed
property is true
.<li>
element now includes a <button>
element with a click
event binding that calls the removeTodo
method passing the item to remove.todos
signal by filtering out the todo item with the matching id
. This effectively removes the specified todo item from the array.toggleTodo
method handles this functionality, ensuring that the todos signal is updated reactively.
The addition of flexbox layout classes further enhances the UI, providing a clean and user-friendly interface.checked
property is bound to the todo.completed
property of the todo, providing visual feedback on the current status.toggleTodo
is invoked each time the checkbox is clickedtodos.map()
is used to iterate over each item in the todos
array. The goal is to create a new array with the updated completed status for the specified todo.t.id === todoToToggle.id ? {...t, completed: !t.completed}
:
For each todo item t
in the array, the code checks if the id
matches the id
of todoToToggle
:t
but with the completed
property toggled.t
.{...t, completed: !t.completed}
:
This syntax uses the spread operator to create a new object. It copies all properties from t
and then overrides the completed
property with its toggled value (!t.completed
).addTodo
method ensures that the new todo is added to the list reactively.#inputRef
is a template reference variable and creates a reference to the input element that can be used within the component's template or passed to the component's methods.(keydown.enter)="addTodo(inputRef)"
is an event binding that listens for the keydown
event, specifically when the Enter
key is pressed.Enter
key is pressed, the addTodo
method of the component is called, with inputRef
(the reference to the input element) passed as an argument.HTMLInputElement
as a parameter, which is the reference to the input field.getElementById
or querySelector
in vanilla JavaScript.id
(using Date.now()
), the title
from the input field's value, and completed
set to false.Date.now()
now ensures that we have always a different id
for each new item<div>
element at the top of the template dynamically displays these counts, enhancing the user interface and providing an immediate overview of the todo list's status.totalCompleted
computed property filters the todos array to include only completed tasks and returns their count.totalTodos
filters the todos array to include only incomplete tasks and returns their count.