Quick Reference Angular Cheatsheet
Modern Angular on one page — standalone components, template bindings, built-in control
flow, signals, services, HttpClient, routing, reactive forms, and the traps that show up
in interviews.
10 sections 10 tables 16 code snippets Start and generate ng new shop
ng serve
ng generate component user-card
ng generate service users
ng build Where configuration lives src/main.ts bootstrap
src/app/app.config.ts providers
src/app/app.routes.ts routes
src/app/app.ts root component NgModules still work. New components should stay standalone. A module is the old way to group declarations and imports. The class import { Component } from '@angular/core';
@Component({
selector: 'app-user-card',
templateUrl: './user-card.component.html',
styleUrl: './user-card.component.css',
})
export class UserCardComponent {} Text, property, event <p>{{ name() }}</p>
<img [src]="photo()" [alt]="name()" />
<button (click)="save()">Save</button>
<input [value]="query()" (input)="onInput($event)" /> Square brackets set a property. Parentheses listen. Writing src="{{ photo() }}" stringifies the value. Prefer [src]="photo()" for anything that is not plain text. @if and @for @if (user(); as current) {
<p>Hello, {{ current.name }}</p>
} @else {
<p>Sign in</p>
}
@for (item of items(); track item.id) {
<li>{{ item.name }}</li>
} @empty {
<li>Nothing yet</li>
} These blocks need no import. *ngIf and *ngFor still work if you import them, but new templates should use @if and @for. Local state count = signal(0);
doubled = computed(() => this.count() * 2);
inc() {
this.count.update((n) => n + 1);
} Read it in the template <button (click)="inc()">{{ count() }}</button>
<p>{{ doubled() }}</p> Replace state. items().push(row) mutates the array and does not notify readers. update with a new array. An app-wide service import { inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({ providedIn: 'root' })
export class UsersService {
private http = inject(HttpClient);
list() {
return this.http.get<User[]>('/api/users');
}
} Call inject() in a field or constructor, not inside save(). The click handler is not an injection context, so a late inject() throws. Register it once export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideHttpClient(),
],
}; Read it as a signal private usersService = inject(UsersService);
users = toSignal(this.usersService.list(), {
initialValue: [],
}); provideHttpClient() comes from @angular/common/http. toSignal comes from @angular/core/rxjs-interop. Without the provider, inject(HttpClient) fails at runtime. Routes export const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'users/:id', component: UserComponent },
]; Links and the outlet <a routerLink="/users/1" routerLinkActive="active">
User
</a>
<router-outlet /> Replace the list add(row: Item) {
this.items.update((list) => [...list, row]);
} Track a stable id @for (item of items(); track item.id) {
<app-row [item]="item" />
} How to use this page: generate a component, give it a signal, and render it with {{ count() }}. Then add @for with track item.id and delete the track clause so the compiler shows you it is required. |