Sub topic 1
Creating the Project with the Angular CLI
Prerequisites
Assume you know JavaScript functions, arrays, objects, and modules. You should also know basic TypeScript, HTML, and CSS, and have Node.js and npm installed.
Create the Angular project
Angular CLI is a command-line tool that creates and manages Angular applications. Run:
npm install -g @angular/cli
ng new task-list
cd task-list
ng serveOpen http://localhost:4200 in a browser. The development server rebuilds the application when you save a file.
Angular creates standalone components by default. A standalone component declares its own template dependencies instead of belonging to an Angular module.
Model a task with TypeScript
An interface describes the shape of an object. Create src/app/task.ts:
export interface Task {
id: number;
title: string;
done: boolean;
}This interface requires every task to have a numeric id, a string title, and a Boolean done value. TypeScript can now detect incorrectly shaped tasks before the application runs.
Create a standalone component
A component combines a class with a template. Replace the generated component with this small example:
import { Component } from '@angular/core';
import { Task } from './task';
@Component({
selector: 'app-root',
template: `
<h1>My tasks</h1>
<ul>
@for (task of tasks; track task.id) {
<li>
<input type="checkbox" [checked]="task.done" />
<span>{{ task.title }}</span>
</li>
} @empty {
<li>No tasks yet.</li>
}
</ul>
`,
})
export class AppComponent {
readonly tasks: Task[] = [
{ id: 1, title: 'Learn Angular', done: false },
{ id: 2, title: 'Build a task list', done: true },
];
}The @Component decorator identifies the class as a component. Its template contains the HTML rendered by that component.
Render the list with bindings and `@for`
{{ task.title }} is interpolation. It reads a value and places its text in the page. [checked]="task.done" is property binding. It sets the checkbox property from the task.
@for repeats markup for each task. The track task.id expression gives Angular a stable identity, so Angular can update only the changed list item. The @empty block renders when the array has no items.
Exercise
What happens if the tasks array is empty?
Show answer
Angular renders the @empty block, displaying “No tasks yet.”