Contenuti del Corso
Introduction to Angular
Introduction to Angular
Implementing the TaskDetailsComponent
On the main page of our app, the user sees a list of all tasks. This makes it easy to quickly get an overview of what needs to be done.
But what if the user wants to see the details of a specific task — its unique ID, exact title, and status? For that, we need to create a separate task details page.
This chapter will show you how to build the TaskDetailsComponent
, which opens when the user navigates to a specific URL and displays information about a particular task.
Creating a New Component
To display task details, we'll create a standalone component. It will be independent and can be connected directly in the routing configuration.
Run this command:
This will create the file task-details.component.ts
along with its template, styles, and tests. You can safely delete the test file if you want.
Implementing the Component
At this stage, we only need the task's id, title, and status — this is all the information we have about a task. So in the component, we'll just add a task
property.
component
import { Component } from '@angular/core'; import { Task } from '../../services/task.service'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-task-details', standalone: true, imports: [CommonModule], templateUrl: './task-details.component.html', styleUrls: ['./task-details.component.css'] }) export class TaskDetailsComponent { task: Task | undefined; }
Don't forget to import CommonModule
in the component, as we'll need it in the template.
Now let's create the template and its CSS styles:
component
component
<div *ngIf="task; else notFound" class="task-details-card"> <h2>Task Details</h2> <p><strong>ID:</strong> {{ task.id }}</p> <p><strong>Title:</strong> {{ task.title }}</p> <p><strong>Status:</strong> {{ task.completed ? 'Completed' : 'Incomplete' }}</p> <button (click)="goToHomePage()">← Back to tasks</button> </div> <ng-template #notFound> <p>Task not found.</p> </ng-template>
The template uses the *ngIf
directive to display the task details card if the task exists; otherwise, it shows a "Task not found" message using ng-template.
Inside the card, it shows the ID, title, and status of the task, plus a button to go back to the main task list (we'll add the button's functionality later).
Now our TaskDetailsComponent
is ready to use. We'll connect it to our routing in the next chapter.
Grazie per i tuoi commenti!