|
| 1 | +--- |
| 2 | +description: 'Angular-specific coding standards and best practices' |
| 3 | +applyTo: '**/*.ts, **/*.html, **/*.scss, **/*.css' |
| 4 | +--- |
| 5 | + |
| 6 | +# Angular Development Instructions |
| 7 | + |
| 8 | +Instructions for generating high-quality Angular applications with TypeScript, using Angular Signals for state management, adhering to Angular best practices as outlined at https://angular.dev. |
| 9 | + |
| 10 | +## Project Context |
| 11 | +- Latest Angular version (use standalone components by default) |
| 12 | +- TypeScript for type safety |
| 13 | +- Angular CLI for project setup and scaffolding |
| 14 | +- Follow Angular Style Guide (https://angular.dev/style-guide) |
| 15 | +- Use Angular Material or other modern UI libraries for consistent styling (if specified) |
| 16 | + |
| 17 | +## Development Standards |
| 18 | + |
| 19 | +### Architecture |
| 20 | +- Use standalone components unless modules are explicitly required |
| 21 | +- Organize code by standalone feature modules or domains for scalability |
| 22 | +- Implement lazy loading for feature modules to optimize performance |
| 23 | +- Use Angular's built-in dependency injection system effectively |
| 24 | +- Structure components with a clear separation of concerns (smart vs. presentational components) |
| 25 | + |
| 26 | +### TypeScript |
| 27 | +- Enable strict mode in `tsconfig.json` for type safety |
| 28 | +- Define clear interfaces and types for components, services, and models |
| 29 | +- Use type guards and union types for robust type checking |
| 30 | +- Implement proper error handling with RxJS operators (e.g., `catchError`) |
| 31 | +- Use typed forms (e.g., `FormGroup`, `FormControl`) for reactive forms |
| 32 | + |
| 33 | +### Component Design |
| 34 | +- Follow Angular's component lifecycle hooks best practices |
| 35 | +- When using Angular >= 19, Use `input()` `output()`, `viewChild()`, `viewChildren()`, `contentChild()` and `contentChildren()` functions instead of decorators; otherwise use decorators |
| 36 | +- Leverage Angular's change detection strategy (default or `OnPush` for performance) |
| 37 | +- Keep templates clean and logic in component classes or services |
| 38 | +- Use Angular directives and pipes for reusable functionality |
| 39 | + |
| 40 | +### Styling |
| 41 | +- Use Angular's component-level CSS encapsulation (default: ViewEncapsulation.Emulated) |
| 42 | +- Prefer SCSS for styling with consistent theming |
| 43 | +- Implement responsive design using CSS Grid, Flexbox, or Angular CDK Layout utilities |
| 44 | +- Follow Angular Material's theming guidelines if used |
| 45 | +- Maintain accessibility (a11y) with ARIA attributes and semantic HTML |
| 46 | + |
| 47 | +### State Management |
| 48 | +- Use Angular Signals for reactive state management in components and services |
| 49 | +- Leverage `signal()`, `computed()`, and `effect()` for reactive state updates |
| 50 | +- Use writable signals for mutable state and computed signals for derived state |
| 51 | +- Handle loading and error states with signals and proper UI feedback |
| 52 | +- Use Angular's `AsyncPipe` to handle observables in templates when combining signals with RxJS |
| 53 | + |
| 54 | +### Data Fetching |
| 55 | +- Use Angular's `HttpClient` for API calls with proper typing |
| 56 | +- Implement RxJS operators for data transformation and error handling |
| 57 | +- Use Angular's `inject()` function for dependency injection in standalone components |
| 58 | +- Implement caching strategies (e.g., `shareReplay` for observables) |
| 59 | +- Store API response data in signals for reactive updates |
| 60 | +- Handle API errors with global interceptors for consistent error handling |
| 61 | + |
| 62 | +### Security |
| 63 | +- Sanitize user inputs using Angular's built-in sanitization |
| 64 | +- Implement route guards for authentication and authorization |
| 65 | +- Use Angular's `HttpInterceptor` for CSRF protection and API authentication headers |
| 66 | +- Validate form inputs with Angular's reactive forms and custom validators |
| 67 | +- Follow Angular's security best practices (e.g., avoid direct DOM manipulation) |
| 68 | + |
| 69 | +### Performance |
| 70 | +- Enable production builds with `ng build --prod` for optimization |
| 71 | +- Use lazy loading for routes to reduce initial bundle size |
| 72 | +- Optimize change detection with `OnPush` strategy and signals for fine-grained reactivity |
| 73 | +- Use trackBy in `ngFor` loops to improve rendering performance |
| 74 | +- Implement server-side rendering (SSR) or static site generation (SSG) with Angular Universal (if specified) |
| 75 | + |
| 76 | +### Testing |
| 77 | +- Write unit tests for components, services, and pipes using Jasmine and Karma |
| 78 | +- Use Angular's `TestBed` for component testing with mocked dependencies |
| 79 | +- Test signal-based state updates using Angular's testing utilities |
| 80 | +- Write end-to-end tests with Cypress or Playwright (if specified) |
| 81 | +- Mock HTTP requests using `provideHttpClientTesting` |
| 82 | +- Ensure high test coverage for critical functionality |
| 83 | + |
| 84 | +## Implementation Process |
| 85 | +1. Plan project structure and feature modules |
| 86 | +2. Define TypeScript interfaces and models |
| 87 | +3. Scaffold components, services, and pipes using Angular CLI |
| 88 | +4. Implement data services and API integrations with signal-based state |
| 89 | +5. Build reusable components with clear inputs and outputs |
| 90 | +6. Add reactive forms and validation |
| 91 | +7. Apply styling with SCSS and responsive design |
| 92 | +8. Implement lazy-loaded routes and guards |
| 93 | +9. Add error handling and loading states using signals |
| 94 | +10. Write unit and end-to-end tests |
| 95 | +11. Optimize performance and bundle size |
| 96 | + |
| 97 | +## Additional Guidelines |
| 98 | +- Follow the Angular Style Guide for file naming conventions (see https://angular.dev/style-guide), e.g., use `feature.ts` for components and `feature-service.ts` for services. For legacy codebases, maintain consistency with existing pattern. |
| 99 | +- Use Angular CLI commands for generating boilerplate code |
| 100 | +- Document components and services with clear JSDoc comments |
| 101 | +- Ensure accessibility compliance (WCAG 2.1) where applicable |
| 102 | +- Use Angular's built-in i18n for internationalization (if specified) |
| 103 | +- Keep code DRY by creating reusable utilities and shared modules |
| 104 | +- Use signals consistently for state management to ensure reactive updates |
0 commit comments