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.
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.
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.
Creating New Projects
If no guidelines are provided by the user, here are some default rules to follow when creating a new Angular project:
Use the latest stable version of Angular unless the user specifies otherwise.
Use Signals Forms for form management in new projects (available in Angular v21 and newer) Find out more.
Execution Rules for ng new:
When asked to create a new Angular project, you must determine the correct execution command by following these strict steps:
Step 1: Check for an explicit user version.
IF the user requests a specific version (e.g., Angular 15), bypass local installations and strictly use npx.
Command:npx @angular/cli@<requested_version> new <project-name>
Step 2: Check for an existing Angular installation.
IF no specific version is requested, run ng version in the terminal to check if the Angular CLI is already installed on the system.
IF the command succeeds and returns an installed version, use the local/global installation directly.
Command:ng new <project-name>
Step 3: Fallback to Latest.
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.
Command:npx @angular/cli@latest new <project-name>
Components
When working with Angular components, consult the following references based on the task:
Fundamentals: Anatomy, metadata, core concepts, and template control flow (@if, @for, @switch). Read components.md
Inputs: Signal-based inputs, transforms, and model inputs. Read inputs.md
Outputs: Signal-based outputs and custom event best practices. Read outputs.md
Host Elements: Host bindings and attribute injection. Read host-elements.md
If you require deeper documentation not found in the references above, read the documentation at https://angular.dev/guide/components.
Reactivity and Data Management
When managing state and data reactivity, use Angular Signals and consult the following references:
Signals Overview: Core signal concepts (signal, computed), reactive contexts, and untracked. Read signals-overview.md
Dependent State (linkedSignal): Creating writable state linked to source signals. Read linked-signal.md
Async Reactivity (resource): Fetching asynchronous data directly into signal state. Read resource.md
Side Effects (effect): Logging, third-party DOM manipulation (afterRenderEffect), and when NOT to use effects. Read effects.md
HTTP Communication
When communicating with backend services, use Angular HTTP APIs and consult the following reference:
HTTP Client and Resources: provideHttpClient, HttpClient, interceptors, and httpResource. Read http-client.md
Forms
In most cases for new apps, prefer signal forms. When making a forms decision, analyze the project and consider the following guidelines:
If the application is using v21 or newer and this is a new form, prefer signal forms.
For older applications or when working with existing forms, use the appropriate form type that matches the applications current form strategy.
Signal Forms: Use signals for form state management. Read signal-forms.md
Template-driven forms: Use for simple forms. Read template-driven-forms.md
Reactive forms: Use for complex forms. Read reactive-forms.md
Dependency Injection
When implementing dependency injection in Angular, follow these guidelines:
Fundamentals: Overview of Dependency Injection, services, and the inject() function. Read di-fundamentals.md
Creating and Using Services: Creating services, the providedIn: 'root' option, and injecting into components or other services. Read creating-services.md
Defining Dependency Providers: Automatic vs manual provision, InjectionToken, useClass, useValue, useFactory, and scopes. Read defining-providers.md
Injection Context: Where inject() is allowed, runInInjectionContext, and assertInInjectionContext. Read injection-context.md
Hierarchical Injectors: The EnvironmentInjector vs ElementInjector, resolution rules, modifiers (optional, skipSelf), and providers vs viewProviders. Read hierarchical-injectors.md
Pipes
When formatting values in templates, creating custom pipes, or reusing pipe-like logic in TypeScript, consult the following reference. Prefer pipes in templates; outside templates, avoid injecting pipe classes just to call transform().
Pipes: Built-in pipe imports, custom pipe naming and implementation, pure vs impure pipes, and TypeScript reuse patterns using standalone formatting functions or extracted plain functions. Read pipes.md
Angular Aria
When building accessible custom components for any of the following patterns: Accordion, Listbox, Combobox, Menu, Tabs, Toolbar, Tree, Grid, consult the following reference: