Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Implementing the TaskDetailsComponent | Routing and Navigation in Angular
Introduction to Angular
course content

Course Content

Introduction to Angular

Introduction to Angular

1. Angular Fundamentals
2. Components and Templates
3. Mastering Angular Directives and Pipes
4. Services and Dependency Injection in Angular
5. Standalone Components & Modules
6. Routing and Navigation in Angular

book
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.

ts

component

copy
1234567891011121314
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; }
Note
Note

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:

html

component

css

component

copy
1234567891011
<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.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 2

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

course content

Course Content

Introduction to Angular

Introduction to Angular

1. Angular Fundamentals
2. Components and Templates
3. Mastering Angular Directives and Pipes
4. Services and Dependency Injection in Angular
5. Standalone Components & Modules
6. Routing and Navigation in Angular

book
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.

ts

component

copy
1234567891011121314
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; }
Note
Note

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:

html

component

css

component

copy
1234567891011
<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.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 2
We're sorry to hear that something went wrong. What happened?
some-alt