Video lecture
Watch on YouTube(opens in a new tab)Sub topic 3
Conditionals and Lists with @if and @for
This chapter assumes you have a generated project, can run ng serve, and know how to write a standalone component with interpolation and event binding.
Conditionals and Lists with @if and @for
Angular templates have built-in control flow blocks. @if renders content when its condition is true, and @else covers the other case. @for repeats content for each item in a list. Every @for needs a track expression, which gives Angular a stable identity for each item so it can reuse DOM nodes. If you leave out track, the template fails to compile. If you track by a value that isn't unique, rows can be reused incorrectly.
Reactive State with Signals
A signal is a value that tells Angular when it changes. You read it by calling it, as in count(). You change it with set() or update(). computed() creates a read-only signal derived from other signals and recalculates when they change. A common mistake is to write count in a template instead of count(), which displays the function rather than its value.
Passing Data Between Components
input() declares a value that the parent passes in, and you read it as a signal. output() declares an event that the component sends back to the parent with emit().
import { Component, input, output } from '@angular/core';
@Component({
selector: 'app-todo-list',
template: `
@if (items().length === 0) {
<p>Nothing to do.</p>
} @else {
<ul>
@for (item of items(); track item) {
<li (click)="selected.emit(item)">{{ item }}</li>
}
</ul>
}
`,
})
export class TodoList {
items = input<string[]>([]);
selected = output<string>();
}A parent uses it like this: <app-todo-list [items]="todos()" (selected)="onPick($event)" />.
Sharing Logic with Services and inject()
A service is a class marked @Injectable. With providedIn: 'root', the whole app shares one instance of it. Components get the instance by calling inject(GreetingService) in a field initializer. inject() only works in an injection context, such as a field initializer or a constructor. Calling it inside a method throws an error.
Adding Pages with the Router
Define Routes in app.routes.ts, register them with provideRouter(routes) in app.config.ts, and place <router-outlet /> in the root template. You also need to import RouterOutlet into the root component's imports array. If you forget, the outlet renders nothing.
Running Unit Tests with ng test
ng test builds the project in test mode and runs each *.spec.ts file. ng generate component home creates a spec file alongside the component. Run ng build to write an optimized production bundle to dist/.
ng generate component home
ng test
ng buildExercise
The template contains @for (user of users()) { <li>{{ user.name }}</li> } and fails to compile. Why, and what is a good fix if each user has a unique id?
Show answer
@for requires a track expression. Write @for (user of users(); track user.id) so Angular identifies each row by its unique id.
Exercise
Inside a click handler you write const svc = inject(GreetingService); and get a runtime error. What should you change?
Show answer
inject() must run in an injection context. Move it to a field initializer, for example private svc = inject(GreetingService);, and use this.svc inside the handler.