--- description: Provides definitive guidelines for modern Angular development (2025), focusing on best practices for code structure, component architecture, state management with signals, performance, and type safety. globs: **/*.{ts,html} --- # Angular Best Practices This guide outlines the definitive best practices for Angular development, ensuring consistency, maintainability, and optimal performance. Adhere to these rules strictly. ## 1. Code Organization and Structure ### 1.1 One Concept Per File (SRP) **Rule:** Each file must contain a single Angular concept (e.g., one component, one directive, one pipe, one service, one interface). **Explanation:** This enforces the Single Responsibility Principle (SRP), improving modularity, readability, and testability. Exceptions are rare and only for tightly coupled, non-exported elements. **❌ BAD:** ```typescript // user.model-and-service.ts export interface User { id: string; name: string; } @Injectable({ providedIn: 'root' }) export class UserService { // ... fetches users } ``` **✅ GOOD:** ```typescript // user.model.ts export interface User { id: string; name: string; } // user.service.ts @Injectable({ providedIn: 'root' }) export class UserService { // ... fetches users } ``` ### 1.2 Consistent Naming Conventions **Rule:** Follow kebab-case for file names (`feature-name.type.ts`) and PascalCase for class names. Use standard type suffixes. **Explanation:** Consistent naming makes files easy to locate and understand at a glance. **❌ BAD:** ```typescript // MyUserList.component.ts export class UserListComp { /* ... */ } // authservice.ts export class AuthenticationService { /* ... */ } ``` **✅ GOOD:** ```typescript // user-list.component.ts export class UserListComponent { /* ... */ } // auth.service.ts export class AuthService { /* ... */ } ``` ## 2. Component Architecture ### 2.1 Standalone Components by Default **Rule:** Always create standalone components, directives, and pipes. Avoid NgModules for new features unless strictly necessary for legacy integration or specific library patterns. **Explanation:** Standalone components simplify the Angular mental model, reduce boilerplate, and improve tree-shaking. **❌ BAD:** ```typescript // app.module.ts @NgModule({ declarations: [MyComponent], imports: [CommonModule], // ... }) export class AppModule { } // my.component.ts @Component({ selector: 'app-my', template: `
My Component
`, styleUrls: ['./my.component.css'], }) export class MyComponent { } ``` **✅ GOOD:** ```typescript // my.component.ts import { Component } from '@angular/core'; import { CommonModule } from '@angular/common'; // Import dependencies directly @Component({ selector: 'app-my', standalone: true, // Always standalone imports: [CommonModule], template: `My Component
`, styleUrls: ['./my.component.css'], }) export class MyComponent { } ``` ### 2.2 Prefer `inject` for Dependency Injection **Rule:** Use the `inject` function for all dependency injection, especially in field initializers. Avoid constructor injection unless required for specific lifecycle hooks or inheritance patterns. **Explanation:** `inject` offers greater flexibility, better tree-shaking, and allows injection outside of constructors. **❌ BAD:** ```typescript import { MyService } from './my.service'; @Component({...}) export class MyComponent { private myService: MyService; constructor(myService: MyService) { this.myService = myService; } } ``` **✅ GOOD:** ```typescript import { inject } from '@angular/core'; import { MyService } from './my.service'; @Component({...}) export class MyComponent { // Use a private readonly field for injected services private readonly myService = inject(MyService); // You can also inject in functions if needed (e.g., providers) // readonly myOtherService = someFactoryFn(inject(AnotherService)); } ``` ## 3. State Management ### 3.1 Prioritize Signals for Component State **Rule:** Use Angular Signals for all local component state and simple derived state. Reserve RxJS for complex asynchronous data streams, side effects, and inter-component communication. **Explanation:** Signals provide a reactive, performant, and simpler way to manage state, optimizing change detection automatically. **❌ BAD:** ```typescript // Using plain properties and relying on Zone.js for change detection @Component({...}) export class MyComponent { count = 0; items: string[] = []; addItem(item: string) { this.items.push(item); // Direct mutation, less reactive } } ``` **✅ GOOD:** ```typescript import { Component, signal, computed } from '@angular/core'; @Component({...}) export class MyComponent { readonly count = signal(0); // Primitive signal readonly items = signalNo items found.
} ``` ### 4.2 Always Use `track` Key in `@for` **Rule:** Always provide a `track` key in `@for` loops. Use a unique, primitive property of the object (e.g., `item.id`). If iterating over primitives, use the value directly. **Explanation:** The `track` key helps Angular optimize rendering by identifying unique items, preventing unnecessary DOM re-renders and improving performance. **❌ BAD:** ```html @for (user of users) {