Components
3 snippetsBuilding blocks of Angular apps
Standalone Component
@Component({
selector: 'app-hello',
standalone: true,
imports: [CommonModule],
template: `<h1>Hello {{ name() }}</h1>`,
})
export class HelloComponent {
name = input<string>('World');
} Signals
count = signal(0);
double = computed(() => this.count() * 2);
increment() { this.count.update(n => n + 1); }
reset() { this.count.set(0); }
constructor() {
effect(() => console.log('Count:', this.count()));
} Input / Output
// Signal-based (modern)
title = input.required<string>();
onSave = output<Post>();
// Decorator-based (classic)
@Input() title: string;
@Output() onSave = new EventEmitter<Post>(); Templates
3 snippetsTemplate syntax and directives
Binding
<!-- Interpolation -->
<p>{{ title }}</p>
<!-- Property -->
<img [src]="imageUrl" />
<!-- Event -->
<button (click)="save()">Save</button>
<!-- Two-way -->
<input [(ngModel)]="name" /> Control Flow (new)
@if (user()) {
<p>Hello {{ user().name }}</p>
} @else {
<p>Please log in</p>
}
@for (item of items(); track item.id) {
<li>{{ item.name }}</li>
} @empty {
<li>No items</li>
} Pipes
{{ price | currency:'USD' }}
{{ date | date:'mediumDate' }}
{{ name | uppercase }}
{{ items | async }}
{{ data | json }} Services & DI
2 snippetsDependency injection
Injectable Service
@Injectable({ providedIn: 'root' })
export class UserService {
private http = inject(HttpClient);
getUsers() {
return this.http.get<User[]>('/api/users');
}
} inject()
export class UserComponent {
private userService = inject(UserService);
private router = inject(Router);
users = toSignal(this.userService.getUsers());
} Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Routing
3 snippetsNavigation and route config
Routes
export const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'posts/:id', component: PostComponent },
{ path: 'admin', loadComponent: () =>
import('./admin/admin.component').then(m => m.AdminComponent),
canActivate: [authGuard]
},
{ path: '**', redirectTo: '' },
]; Router Link
<a routerLink="/posts" routerLinkActive="active">Posts</a>
<a [routerLink]="['/posts', post.id]">{{ post.title }}</a> Programmatic
private router = inject(Router);
this.router.navigate(['/posts', id]);
this.router.navigate(['/search'], { queryParams: { q: 'angular' } }); Reactive Forms
3 snippetsForm handling with FormBuilder
FormGroup
private fb = inject(FormBuilder);
form = this.fb.group({
name: ['', [Validators.required, Validators.minLength(3)]],
email: ['', [Validators.required, Validators.email]],
role: ['user'],
}); Template
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<input formControlName="name" />
@if (form.get('name')?.errors?.['required']) {
<span>Name required</span>
}
<button [disabled]="form.invalid">Submit</button>
</form> Submit
onSubmit() {
if (this.form.valid) {
const data = this.form.getRawValue();
this.userService.create(data).subscribe();
}
} HTTP Client
2 snippetsAPI calls and interceptors
GET / POST
private http = inject(HttpClient);
getPosts(): Observable<Post[]> {
return this.http.get<Post[]>('/api/posts');
}
createPost(data: PostDTO): Observable<Post> {
return this.http.post<Post>('/api/posts', data);
} Interceptor
export const authInterceptor: HttpInterceptorFn = (req, next) => {
const token = inject(AuthService).getToken();
const authReq = req.clone({
setHeaders: { Authorization: 'Bearer ' + token }
});
return next(authReq);
};