Angular Conventions
Project rules (lint config, existing patterns) override these.
Components:
- Standalone (default since v19 — don't write
standalone: true). AlwaysChangeDetectionStrategy.OnPush. - Import only the pipes/directives used, never
CommonModule. - Host bindings in
host: {}metadata, not@HostBinding/@HostListener. - Template-only members
protected; internals#private.
Signals:
input(),input.required(),output(),model()— no@Input/@Output, noSignalsuffix, no aliases.- State
signal(), derivedcomputed(), writable derivedlinkedSignal(). - Update with
set/updatereturning new objects/arrays — never mutate. effect()only for real side effects (DOM libs, analytics), never to copy state between signals.
DI:
readonly #api: ApiService = inject(ApiService);— empty or no constructor.- Stateless services
providedIn: 'root'; config through typedInjectionToken<T>.
Templates:
@if,@for,@switchonly. Every@forhastrack item.id($indexonly for primitives); add@emptywhere a list can be empty.- No method calls in bindings (
{{ total() }}of acomputedis fine,{{ calcTotal() }}is not) — usecomputed()or a pure pipe. [class.x]/[style.x]instead ofngClass/ngStyle.
Types and errors:
- No
any. External data isunknown, narrowed by a type guard or schema (Zod/Valibot). - No silent
catchError(() => of(null))— log and expose an error state. Noconsole.log.
@Component({
selector: 'app-user-card',
templateUrl: './user-card.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
host: { '[class.active]': 'active()', '(click)': 'select.emit(user().id)' },
})
export class UserCardComponent {
readonly user = input.required<User>();
readonly active = input(false);
readonly select = output<string>();
readonly #format: NameFormatter = inject(NameFormatter);
protected readonly name = computed(() => this.#format.full(this.user()));
}Tests: *.spec.ts next to the file; test inputs, outputs and user interaction, not private details.