|
| 1 | +--- |
| 2 | +name: angular-developer |
| 3 | +description: Generates Angular code and provides architectural guidance. Trigger when creating projects, components, or services, or for best practices on reactivity (signals, linkedSignal, resource), forms, dependency injection, routing, SSR, accessibility (ARIA), animations, styling (component styles, Tailwind CSS), testing, or CLI tooling. |
| 4 | +license: MIT |
| 5 | +metadata: |
| 6 | + author: Copyright 2026 Google LLC |
| 7 | + version: '1.0' |
| 8 | +--- |
| 9 | + |
| 10 | +# Angular Developer Guidelines |
| 11 | + |
| 12 | +1. Always analyze the project's Angular version before providing guidance, as best practices and available features can vary significantly between versions. If creating a new project with Angular CLI, do not specify a version unless prompted by the user. |
| 13 | + |
| 14 | +2. When generating code, follow Angular's style guide and best practices for maintainability and performance. Use the Angular CLI for scaffolding components, services, directives, pipes, and routes to ensure consistency. |
| 15 | + |
| 16 | +3. Once you finish generating code, run `ng build` to ensure there are no build errors. If there are errors, analyze the error messages and fix them before proceeding. Do not skip this step, as it is critical for ensuring the generated code is correct and functional. |
| 17 | + |
| 18 | +## Creating New Projects |
| 19 | + |
| 20 | +If no guidelines are provided by the user, here are same default rules to follow when creating a new Angular project: |
| 21 | + |
| 22 | +1. Use the latest stable version of Angular unless the user specifies otherwise. |
| 23 | +2. Use Signals Forms for form management in new projects (available in Angular v21 and newer) [Find out more](references/signal-forms.md). |
| 24 | + |
| 25 | +**Execution Rules for `ng new`:** |
| 26 | +When asked to create a new Angular project, you must determine the correct execution command by following these strict steps: |
| 27 | + |
| 28 | +**Step 1: Check for an explicit user version.** |
| 29 | + |
| 30 | +- **IF** the user requests a specific version (e.g., Angular 15), bypass local installations and strictly use `npx`. |
| 31 | +- **Command:** `npx @angular/cli@<requested_version> new <project-name>` |
| 32 | + |
| 33 | +**Step 2: Check for an existing Angular installation.** |
| 34 | + |
| 35 | +- **IF** no specific version is requested, run `ng version` in the terminal to check if the Angular CLI is already installed on the system. |
| 36 | +- **IF** the command succeeds and returns an installed version, use the local/global installation directly. |
| 37 | +- **Command:** `ng new <project-name>` |
| 38 | + |
| 39 | +**Step 3: Fallback to Latest.** |
| 40 | + |
| 41 | +- **IF** no specific version is requested AND the `ng version` command fails (indicating no Angular installation exists), you must use `npx` to fetch the latest version. |
| 42 | +- **Command:** `npx @angular/cli@latest new <project-name>` |
| 43 | + |
| 44 | +## Components |
| 45 | + |
| 46 | +When working with Angular components, consult the following references based on the task: |
| 47 | + |
| 48 | +- **Fundamentals**: Anatomy, metadata, core concepts, and template control flow (@if, @for, @switch). Read [components.md](references/components.md) |
| 49 | +- **Inputs**: Signal-based inputs, transforms, and model inputs. Read [inputs.md](references/inputs.md) |
| 50 | +- **Outputs**: Signal-based outputs and custom event best practices. Read [outputs.md](references/outputs.md) |
| 51 | +- **Host Elements**: Host bindings and attribute injection. Read [host-elements.md](references/host-elements.md) |
| 52 | + |
| 53 | +If you require deeper documentation not found in the references above, read the documentation at `https://angular.dev/guide/components`. |
| 54 | + |
| 55 | +## Reactivity and Data Management |
| 56 | + |
| 57 | +When managing state and data reactivity, use Angular Signals and consult the following references: |
| 58 | + |
| 59 | +- **Signals Overview**: Core signal concepts (`signal`, `computed`), reactive contexts, and `untracked`. Read [signals-overview.md](references/signals-overview.md) |
| 60 | +- **Dependent State (`linkedSignal`)**: Creating writable state linked to source signals. Read [linked-signal.md](references/linked-signal.md) |
| 61 | +- **Async Reactivity (`resource`)**: Fetching asynchronous data directly into signal state. Read [resource.md](references/resource.md) |
| 62 | +- **Side Effects (`effect`)**: Logging, third-party DOM manipulation (`afterRenderEffect`), and when NOT to use effects. Read [effects.md](references/effects.md) |
| 63 | + |
| 64 | +## Forms |
| 65 | + |
| 66 | +In most cases for new apps, **prefer signal forms**. When making a forms decision, analyze the project and consider the following guidelines: |
| 67 | + |
| 68 | +- if the application is using v21 or newer and this is a new form, **prefer signal forms**. |
| 69 | + -For older applications or when working with existing forms, use the appropriate form type that matches the applications current form strategy. |
| 70 | + |
| 71 | +- **Signal Forms**: Use signals for form state management. Read [signal-forms.md](references/signal-forms.md) |
| 72 | +- **Template-driven forms**: Use for simple forms. Read [template-driven-forms.md](references/template-driven-forms.md) |
| 73 | +- **Reactive forms**: Use for complex forms. Read [reactive-forms.md](references/reactive-forms.md) |
| 74 | + |
| 75 | +## Dependency Injection |
| 76 | + |
| 77 | +When implementing dependency injection in Angular, follow these guidelines: |
| 78 | + |
| 79 | +- **Fundamentals**: Overview of Dependency Injection, services, and the `inject()` function. Read [di-fundamentals.md](references/di-fundamentals.md) |
| 80 | +- **Creating and Using Services**: Creating services, the `providedIn: 'root'` option, and injecting into components or other services. Read [creating-services.md](references/creating-services.md) |
| 81 | +- **Defining Dependency Providers**: Automatic vs manual provision, `InjectionToken`, `useClass`, `useValue`, `useFactory`, and scopes. Read [defining-providers.md](references/defining-providers.md) |
| 82 | +- **Injection Context**: Where `inject()` is allowed, `runInInjectionContext`, and `assertInInjectionContext`. Read [injection-context.md](references/injection-context.md) |
| 83 | +- **Hierarchical Injectors**: The `EnvironmentInjector` vs `ElementInjector`, resolution rules, modifiers (`optional`, `skipSelf`), and `providers` vs `viewProviders`. Read [hierarchical-injectors.md](references/hierarchical-injectors.md) |
| 84 | + |
| 85 | +## Angular Aria |
| 86 | + |
| 87 | +When building accessible custom components for any of the following patterns: Accordion, Listbox, Combobox, Menu, Tabs, Toolbar, Tree, Grid, consult the following reference: |
| 88 | + |
| 89 | +- **Angular Aria Components**: Building headless, accessible components (Accordion, Listbox, Combobox, Menu, Tabs, Toolbar, Tree, Grid) and styling ARIA attributes. Read [angular-aria.md](references/angular-aria.md) |
| 90 | + |
| 91 | +## Routing |
| 92 | + |
| 93 | +When implementing navigation in Angular, consult the following references: |
| 94 | + |
| 95 | +- **Define Routes**: URL paths, static vs dynamic segments, wildcards, and redirects. Read [define-routes.md](references/define-routes.md) |
| 96 | +- **Route Loading Strategies**: Eager vs lazy loading, and context-aware loading. Read [loading-strategies.md](references/loading-strategies.md) |
| 97 | +- **Show Routes with Outlets**: Using `<router-outlet>`, nested outlets, and named outlets. Read [show-routes-with-outlets.md](references/show-routes-with-outlets.md) |
| 98 | +- **Navigate to Routes**: Declarative navigation with `RouterLink` and programmatic navigation with `Router`. Read [navigate-to-routes.md](references/navigate-to-routes.md) |
| 99 | +- **Control Route Access with Guards**: Implementing `CanActivate`, `CanMatch`, and other guards for security. Read [route-guards.md](references/route-guards.md) |
| 100 | +- **Data Resolvers**: Pre-fetching data before route activation with `ResolveFn`. Read [data-resolvers.md](references/data-resolvers.md) |
| 101 | +- **Router Lifecycle and Events**: Chronological order of navigation events and debugging. Read [router-lifecycle.md](references/router-lifecycle.md) |
| 102 | +- **Rendering Strategies**: CSR, SSG (Prerendering), and SSR with hydration. Read [rendering-strategies.md](references/rendering-strategies.md) |
| 103 | +- **Route Transition Animations**: Enabling and customizing the View Transitions API. Read [route-animations.md](references/route-animations.md) |
| 104 | + |
| 105 | +If you require deeper documentation or more context, visit the [official Angular Routing guide](https://angular.dev/guide/routing). |
| 106 | + |
| 107 | +## Styling and Animations |
| 108 | + |
| 109 | +When implementing styling and animations in Angular, consult the following references: |
| 110 | + |
| 111 | +- **Using Tailwind CSS with Angular**: Integrating Tailwind CSS into Angular projects. Read [tailwind-css.md](references/tailwind-css.md) |
| 112 | +- **Angular Animations**: Using native CSS (recommended) or the legacy DSL for dynamic effects. Read [angular-animations.md](references/angular-animations.md) |
| 113 | +- **Styling components**: Best practices for component styles and encapsulation. Read [component-styling.md](references/component-styling.md) |
| 114 | + |
| 115 | +## Testing |
| 116 | + |
| 117 | +When writing or updating tests, consult the following references based on the task: |
| 118 | + |
| 119 | +- **Fundamentals**: Best practices for unit testing (Vitest), async patterns, and `TestBed`. Read [testing-fundamentals.md](references/testing-fundamentals.md) |
| 120 | +- **Component Harnesses**: Standard patterns for robust component interaction. Read [component-harnesses.md](references/component-harnesses.md) |
| 121 | +- **Router Testing**: Using `RouterTestingHarness` for reliable navigation tests. Read [router-testing.md](references/router-testing.md) |
| 122 | +- **End-to-End (E2E) Testing**: Best practices for E2E tests with Cypress. Read [e2e-testing.md](references/e2e-testing.md) |
| 123 | + |
| 124 | +## Tooling |
| 125 | + |
| 126 | +When working with Angular tooling, consult the following references: |
| 127 | + |
| 128 | +- **Angular CLI**: Creating applications, generating code (components, routes, services), serving, and building. Read [cli.md](references/cli.md) |
| 129 | +- **Angular MCP Server**: Available tools, configuration, and experimental features. Read [mcp.md](references/mcp.md) |
0 commit comments