@anglr/select 16.0.3 → 16.0.4-beta.20260511085948
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/skills/angular-developer/SKILL.md +130 -0
- package/.claude/skills/angular-developer/references/angular-animations.md +160 -0
- package/.claude/skills/angular-developer/references/angular-aria.md +410 -0
- package/.claude/skills/angular-developer/references/cli.md +86 -0
- package/.claude/skills/angular-developer/references/component-harnesses.md +59 -0
- package/.claude/skills/angular-developer/references/component-styling.md +91 -0
- package/.claude/skills/angular-developer/references/components.md +117 -0
- package/.claude/skills/angular-developer/references/creating-services.md +97 -0
- package/.claude/skills/angular-developer/references/data-resolvers.md +69 -0
- package/.claude/skills/angular-developer/references/define-routes.md +67 -0
- package/.claude/skills/angular-developer/references/defining-providers.md +72 -0
- package/.claude/skills/angular-developer/references/di-fundamentals.md +120 -0
- package/.claude/skills/angular-developer/references/e2e-testing.md +66 -0
- package/.claude/skills/angular-developer/references/effects.md +83 -0
- package/.claude/skills/angular-developer/references/hierarchical-injectors.md +43 -0
- package/.claude/skills/angular-developer/references/host-elements.md +80 -0
- package/.claude/skills/angular-developer/references/injection-context.md +63 -0
- package/.claude/skills/angular-developer/references/inputs.md +101 -0
- package/.claude/skills/angular-developer/references/linked-signal.md +59 -0
- package/.claude/skills/angular-developer/references/loading-strategies.md +61 -0
- package/.claude/skills/angular-developer/references/mcp.md +106 -0
- package/.claude/skills/angular-developer/references/migrations.md +30 -0
- package/.claude/skills/angular-developer/references/navigate-to-routes.md +69 -0
- package/.claude/skills/angular-developer/references/outputs.md +86 -0
- package/.claude/skills/angular-developer/references/reactive-forms.md +122 -0
- package/.claude/skills/angular-developer/references/rendering-strategies.md +44 -0
- package/.claude/skills/angular-developer/references/resource.md +77 -0
- package/.claude/skills/angular-developer/references/route-animations.md +56 -0
- package/.claude/skills/angular-developer/references/route-guards.md +52 -0
- package/.claude/skills/angular-developer/references/router-lifecycle.md +45 -0
- package/.claude/skills/angular-developer/references/router-testing.md +87 -0
- package/.claude/skills/angular-developer/references/show-routes-with-outlets.md +68 -0
- package/.claude/skills/angular-developer/references/signal-forms.md +897 -0
- package/.claude/skills/angular-developer/references/signals-overview.md +94 -0
- package/.claude/skills/angular-developer/references/tailwind-css.md +69 -0
- package/.claude/skills/angular-developer/references/template-driven-forms.md +114 -0
- package/.claude/skills/angular-developer/references/testing-fundamentals.md +66 -0
- package/.github/instructions/typescript/code-conventions.md +8 -0
- package/.github/instructions/typescript/comments.md +41 -0
- package/.github/instructions/typescript/formatting.md +42 -0
- package/.github/instructions/typescript/naming-conventions.md +7 -0
- package/.github/instructions/typescript.instructions.md +26 -0
- package/package.json +1 -1
- package/readme.md +1288 -99
- package/version.bak +1 -1
- package/.github/copilot-instructions.md +0 -1
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# Angular CLI MCP Server
|
|
2
|
+
|
|
3
|
+
The Angular CLI includes a Model Context Protocol (MCP) server that enables AI assistants (like Cursor, Gemini CLI, JetBrains AI, etc.) to interact directly with the Angular CLI. It provides tools for project analysis, guided migrations, and running builds/tests.
|
|
4
|
+
|
|
5
|
+
## Available Tools (Default)
|
|
6
|
+
|
|
7
|
+
When the MCP server is enabled, AI agents have access to the following tools:
|
|
8
|
+
|
|
9
|
+
| Name | Description |
|
|
10
|
+
| :-------------------------- | :-------------------------------------------------------------------------------------------------------- |
|
|
11
|
+
| `ai_tutor` | Launches an interactive AI-powered Angular tutor. |
|
|
12
|
+
| `get_best_practices` | Retrieves the Angular Best Practices Guide (crucial for standalone components, typed forms, etc.). |
|
|
13
|
+
| `list_projects` | Lists all applications and libraries in the workspace by reading `angular.json`. |
|
|
14
|
+
| `onpush_zoneless_migration` | Analyzes code and provides a plan to migrate it to `OnPush` change detection (prerequisite for zoneless). |
|
|
15
|
+
| `search_documentation` | Searches the official documentation at `https://angular.dev`. |
|
|
16
|
+
|
|
17
|
+
## Experimental Tools
|
|
18
|
+
|
|
19
|
+
Some tools must be enabled explicitly using the `--experimental-tool` (or `-E`) flag.
|
|
20
|
+
|
|
21
|
+
| Name | Description |
|
|
22
|
+
| :------------------------- | :-------------------------------------------------------------------- |
|
|
23
|
+
| `build` | Performs a one-off build using `ng build`. |
|
|
24
|
+
| `devserver.start` | Asynchronously starts a dev server (`ng serve`). Returns immediately. |
|
|
25
|
+
| `devserver.stop` | Stops the dev server. |
|
|
26
|
+
| `devserver.wait_for_build` | Returns the logs of the most recent build in a running dev server. |
|
|
27
|
+
| `e2e` | Executes end-to-end tests. |
|
|
28
|
+
| `test` | Runs the project's unit tests. |
|
|
29
|
+
|
|
30
|
+
## Configuration
|
|
31
|
+
|
|
32
|
+
To use the MCP server, you configure your host environment (IDE or CLI) to run `npx @angular/cli mcp`.
|
|
33
|
+
|
|
34
|
+
### Antigravity IDE
|
|
35
|
+
|
|
36
|
+
Create a file named `.antigravity/mcp.json` in your project's root:
|
|
37
|
+
|
|
38
|
+
```json
|
|
39
|
+
{
|
|
40
|
+
"mcpServers": {
|
|
41
|
+
"angular-cli": {
|
|
42
|
+
"command": "npx",
|
|
43
|
+
"args": ["-y", "@angular/cli", "mcp"]
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Gemini CLI
|
|
50
|
+
|
|
51
|
+
Create `.gemini/settings.json` in the project root:
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"mcpServers": {
|
|
56
|
+
"angular-cli": {
|
|
57
|
+
"command": "npx",
|
|
58
|
+
"args": ["-y", "@angular/cli", "mcp"]
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Cursor
|
|
65
|
+
|
|
66
|
+
Create `.cursor/mcp.json` in the project root (or globally at `~/.cursor/mcp.json`):
|
|
67
|
+
|
|
68
|
+
```json
|
|
69
|
+
{
|
|
70
|
+
"mcpServers": {
|
|
71
|
+
"angular-cli": {
|
|
72
|
+
"command": "npx",
|
|
73
|
+
"args": ["-y", "@angular/cli", "mcp"]
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### VS Code
|
|
80
|
+
|
|
81
|
+
Create `.vscode/mcp.json`:
|
|
82
|
+
|
|
83
|
+
```json
|
|
84
|
+
{
|
|
85
|
+
"servers": {
|
|
86
|
+
"angular-cli": {
|
|
87
|
+
"command": "npx",
|
|
88
|
+
"args": ["-y", "@angular/cli", "mcp"]
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Command Options
|
|
95
|
+
|
|
96
|
+
You can pass arguments to the MCP server in the `args` array of your configuration:
|
|
97
|
+
|
|
98
|
+
- `--read-only`: Only registers tools that do not modify the project.
|
|
99
|
+
- `--local-only`: Only registers tools that do not require an internet connection.
|
|
100
|
+
- `--experimental-tool` (`-E`): Enables specific experimental tools (e.g., `-E build`, `-E devserver`).
|
|
101
|
+
|
|
102
|
+
Example for read-only mode with experimental tools enabled:
|
|
103
|
+
|
|
104
|
+
```json
|
|
105
|
+
"args": ["-y", "@angular/cli", "mcp", "--read-only", "-E", "build", "-E", "test"]
|
|
106
|
+
```
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Automatic Migrations & Code Modernization
|
|
2
|
+
|
|
3
|
+
When tasked with refactoring or modernizing an existing codebase, always prefer using the official automated schematics available in `@angular/core` over manual text replacement.
|
|
4
|
+
|
|
5
|
+
## Discovering Migrations
|
|
6
|
+
|
|
7
|
+
To view all available schematics for the installed version of the core framework, run:
|
|
8
|
+
`ng generate @angular/core: --help`
|
|
9
|
+
|
|
10
|
+
## Common Migration Schematics
|
|
11
|
+
|
|
12
|
+
Use the following commands to apply specific syntax updates. You can scope these commands to a specific project or directory using the `--project <name>` or `--path <dir>` flags.
|
|
13
|
+
|
|
14
|
+
| Feature to Modernize | Command to Execute |
|
|
15
|
+
| :------------------------ | :---------------------------------------------------------- |
|
|
16
|
+
| **Built-in Control Flow** | `ng generate @angular/core:control-flow` |
|
|
17
|
+
| **Signal-based Inputs** | `ng generate @angular/core:signal-input-migration` |
|
|
18
|
+
| **Signal Queries** | `ng generate @angular/core:signal-queries-migration` |
|
|
19
|
+
| **Functional Outputs** | `ng generate @angular/core:output-migration` |
|
|
20
|
+
| **`inject()` Function** | `ng generate @angular/core:inject` |
|
|
21
|
+
| **Self-Closing Tags** | `ng generate @angular/core:self-closing-tag` |
|
|
22
|
+
| **Standalone** | `ng generate @angular/core:standalone` (See workflow below) |
|
|
23
|
+
|
|
24
|
+
## Specialized Workflow: Migrating to Standalone
|
|
25
|
+
|
|
26
|
+
The Standalone migration is an interactive, multi-step refactoring. You **MUST** perform this in three discrete stages, verifying that the application builds and runs correctly after each stage completes:
|
|
27
|
+
|
|
28
|
+
1. **Phase 1**: Run `ng generate @angular/core:standalone` and select the option to **Convert all components, directives, and pipes to standalone**.
|
|
29
|
+
2. **Phase 2**: Verify the build with `ng build`. Run the command again and select **Remove unnecessary NgModule classes**.
|
|
30
|
+
3. **Phase 3**: Verify the build with `ng build`. Run the final pass and select **Bootstrap the project using standalone APIs**.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# Navigate to Routes
|
|
2
|
+
|
|
3
|
+
Angular provides both declarative and programmatic ways to navigate between routes.
|
|
4
|
+
|
|
5
|
+
## Declarative Navigation (`RouterLink`)
|
|
6
|
+
|
|
7
|
+
Use the `RouterLink` directive on anchor elements.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import {RouterLink, RouterLinkActive} from '@angular/router';
|
|
11
|
+
|
|
12
|
+
@Component({
|
|
13
|
+
imports: [RouterLink, RouterLinkActive],
|
|
14
|
+
template: `
|
|
15
|
+
<nav>
|
|
16
|
+
<a routerLink="/dashboard" routerLinkActive="active-link">Dashboard</a>
|
|
17
|
+
<a [routerLink]="['/user', userId]">Profile</a>
|
|
18
|
+
</nav>
|
|
19
|
+
`,
|
|
20
|
+
})
|
|
21
|
+
export class Nav {
|
|
22
|
+
userId = '123';
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- **Absolute Paths**: Start with `/` (e.g., `/settings`).
|
|
27
|
+
- **Relative Paths**: No leading `/`. Use `../` to go up a level.
|
|
28
|
+
|
|
29
|
+
## Programmatic Navigation (`Router`)
|
|
30
|
+
|
|
31
|
+
Inject the `Router` service to navigate via TypeScript code.
|
|
32
|
+
|
|
33
|
+
### `router.navigate()`
|
|
34
|
+
|
|
35
|
+
Uses an array of commands.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
private router = inject(Router);
|
|
39
|
+
private route = inject(ActivatedRoute);
|
|
40
|
+
|
|
41
|
+
// Standard navigation
|
|
42
|
+
this.router.navigate(['/profile']);
|
|
43
|
+
|
|
44
|
+
// With parameters
|
|
45
|
+
this.router.navigate(['/search'], {
|
|
46
|
+
queryParams: { q: 'angular' },
|
|
47
|
+
fragment: 'results'
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Relative navigation
|
|
51
|
+
this.router.navigate(['edit'], { relativeTo: this.route });
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### `router.navigateByUrl()`
|
|
55
|
+
|
|
56
|
+
Uses a string path. Ideal for absolute navigation or full URLs.
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
this.router.navigateByUrl('/products/123?view=details');
|
|
60
|
+
|
|
61
|
+
// Replace current entry in history
|
|
62
|
+
this.router.navigateByUrl('/login', {replaceUrl: true});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## URL Parameters
|
|
66
|
+
|
|
67
|
+
- **Route Params**: Part of the path (e.g., `/user/123`).
|
|
68
|
+
- **Query Params**: After the `?` (e.g., `/search?q=query`).
|
|
69
|
+
- **Matrix Params**: Scoped to a segment (e.g., `/products;category=books`).
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Outputs (Custom Events)
|
|
2
|
+
|
|
3
|
+
Outputs allow a child component to emit custom events that a parent component can listen to. Angular recommends using the new `output()` function for modern applications.
|
|
4
|
+
|
|
5
|
+
## Function-based outputs
|
|
6
|
+
|
|
7
|
+
Declare outputs using the `output()` function. This returns an `OutputEmitterRef`.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import {Component, output} from '@angular/core';
|
|
11
|
+
|
|
12
|
+
@Component({
|
|
13
|
+
selector: 'custom-slider',
|
|
14
|
+
template: `<button (click)="changeValue(50)">Set to 50</button>`,
|
|
15
|
+
})
|
|
16
|
+
export class CustomSlider {
|
|
17
|
+
// Output without event data
|
|
18
|
+
panelClosed = output<void>();
|
|
19
|
+
|
|
20
|
+
// Output with event data (number)
|
|
21
|
+
valueChanged = output<number>();
|
|
22
|
+
|
|
23
|
+
changeValue(newValue: number) {
|
|
24
|
+
this.valueChanged.emit(newValue);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Usage in Template
|
|
30
|
+
|
|
31
|
+
Bind to the output event using parentheses `()`. If the event emits data, access it using the special `$event` variable.
|
|
32
|
+
|
|
33
|
+
```html
|
|
34
|
+
<custom-slider (panelClosed)="savePanelState()" (valueChanged)="logValue($event)" />
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Configuration Options
|
|
38
|
+
|
|
39
|
+
The `output` function accepts a config object to specify an alias.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
@Component({...})
|
|
43
|
+
export class CustomSlider {
|
|
44
|
+
// The event is named 'valueChanged' in the template,
|
|
45
|
+
// but accessed as 'changed' in the component class.
|
|
46
|
+
changed = output<number>({ alias: 'valueChanged' });
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Programmatic Subscription
|
|
51
|
+
|
|
52
|
+
When creating components dynamically, you can subscribe to outputs programmatically:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
const componentRef = viewContainerRef.createComponent(CustomSlider);
|
|
56
|
+
|
|
57
|
+
const subscription = componentRef.instance.valueChanged.subscribe((val) => {
|
|
58
|
+
console.log('Value changed:', val);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Clean up manually if needed (Angular cleans up destroyed components automatically)
|
|
62
|
+
subscription.unsubscribe();
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Decorator-based Outputs (@Output)
|
|
66
|
+
|
|
67
|
+
The legacy API uses the `@Output()` decorator with an `EventEmitter`. It remains supported but is not recommended for new code.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { Component, Output, EventEmitter } from '@angular/core';
|
|
71
|
+
|
|
72
|
+
@Component({...})
|
|
73
|
+
export class LegacyExample {
|
|
74
|
+
@Output() valueChanged = new EventEmitter<number>();
|
|
75
|
+
|
|
76
|
+
// With alias
|
|
77
|
+
@Output('customEventName') changed = new EventEmitter<void>();
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Best Practices
|
|
82
|
+
|
|
83
|
+
- **Prefer `output()`**: Use the function-based `output()` instead of `@Output()` and `EventEmitter`.
|
|
84
|
+
- **Naming**: Use `camelCase` for output names. Avoid prefixing with `on` (e.g., use `valueChanged` instead of `onValueChanged`).
|
|
85
|
+
- **No DOM Bubbling**: Angular custom events do not bubble up the DOM tree like native events.
|
|
86
|
+
- **Avoid Collisions**: Do not choose names that collide with native DOM events (like `click` or `submit`).
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Reactive Forms
|
|
2
|
+
|
|
3
|
+
Reactive forms provide a model-driven approach to handling form inputs. They are built around observable streams and provide synchronous access to the data model, making them more scalable and testable than template-driven forms.
|
|
4
|
+
|
|
5
|
+
## Core Classes
|
|
6
|
+
|
|
7
|
+
Reactive forms are built using these fundamental classes from `@angular/forms`:
|
|
8
|
+
|
|
9
|
+
- `FormControl`: Manages the value and validity of an individual input.
|
|
10
|
+
- `FormGroup`: Manages a group of controls (an object-like structure).
|
|
11
|
+
- `FormArray`: Manages a numerically indexed array of controls.
|
|
12
|
+
- `FormBuilder`: A service that provides factory methods for creating control instances.
|
|
13
|
+
|
|
14
|
+
## Setup
|
|
15
|
+
|
|
16
|
+
Import `ReactiveFormsModule` into your component.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import {Component, inject} from '@angular/core';
|
|
20
|
+
import {ReactiveFormsModule, FormGroup, FormControl, Validators, FormBuilder} from '@angular/forms';
|
|
21
|
+
|
|
22
|
+
@Component({
|
|
23
|
+
selector: 'app-profile-editor',
|
|
24
|
+
imports: [ReactiveFormsModule],
|
|
25
|
+
templateUrl: './profile-editor.component.html',
|
|
26
|
+
})
|
|
27
|
+
export class ProfileEditor {
|
|
28
|
+
private fb = inject(FormBuilder);
|
|
29
|
+
|
|
30
|
+
// Using FormBuilder for concise definition
|
|
31
|
+
profileForm = this.fb.group({
|
|
32
|
+
firstName: ['', Validators.required],
|
|
33
|
+
lastName: [''],
|
|
34
|
+
address: this.fb.group({
|
|
35
|
+
street: [''],
|
|
36
|
+
city: [''],
|
|
37
|
+
}),
|
|
38
|
+
aliases: this.fb.array([this.fb.control('')]),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
onSubmit() {
|
|
42
|
+
console.warn(this.profileForm.value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Template Binding
|
|
48
|
+
|
|
49
|
+
Use directives to bind the model to the view:
|
|
50
|
+
|
|
51
|
+
- `[formGroup]`: Binds a `FormGroup` to a `<form>` or `<div>`.
|
|
52
|
+
- `formControlName`: Binds a named control within a group to an input.
|
|
53
|
+
- `formGroupName`: Binds a nested `FormGroup`.
|
|
54
|
+
- `formArrayName`: Binds a nested `FormArray`.
|
|
55
|
+
- `[formControl]`: Binds a standalone `FormControl`.
|
|
56
|
+
|
|
57
|
+
```html
|
|
58
|
+
<form [formGroup]="profileForm" (ngSubmit)="onSubmit()">
|
|
59
|
+
<input type="text" formControlName="firstName" />
|
|
60
|
+
|
|
61
|
+
<div formGroupName="address">
|
|
62
|
+
<input type="text" formControlName="street" />
|
|
63
|
+
</div>
|
|
64
|
+
|
|
65
|
+
<div formArrayName="aliases">
|
|
66
|
+
@for (alias of aliases.controls; track $index) {
|
|
67
|
+
<input type="text" [formControlName]="$index" />
|
|
68
|
+
}
|
|
69
|
+
</div>
|
|
70
|
+
|
|
71
|
+
<button type="submit" [disabled]="!profileForm.valid">Submit</button>
|
|
72
|
+
</form>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Accessing Controls
|
|
76
|
+
|
|
77
|
+
Use getters for easy access to controls, especially for `FormArray`.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
get aliases() {
|
|
81
|
+
return this.profileForm.get('aliases') as FormArray;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
addAlias() {
|
|
85
|
+
this.aliases.push(this.fb.control(''));
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Updating Values
|
|
90
|
+
|
|
91
|
+
- `patchValue()`: Updates only the specified properties. Fails silently on structural mismatches.
|
|
92
|
+
- `setValue()`: Replaces the entire model. Strictly enforces the form structure.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
updateProfile() {
|
|
96
|
+
this.profileForm.patchValue({
|
|
97
|
+
firstName: 'Nancy',
|
|
98
|
+
address: { street: '123 Drew Street' }
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Unified Change Events
|
|
104
|
+
|
|
105
|
+
Modern Angular (v18+) provides a single `events` observable on all controls to track value, status, pristine, touched, reset, and submit events.
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import {ValueChangeEvent, StatusChangeEvent} from '@angular/forms';
|
|
109
|
+
|
|
110
|
+
this.profileForm.events.subscribe((event) => {
|
|
111
|
+
if (event instanceof ValueChangeEvent) {
|
|
112
|
+
console.log('New value:', event.value);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Manual State Management
|
|
118
|
+
|
|
119
|
+
- `markAsTouched()` / `markAllAsTouched()`: Useful for showing validation errors on submit.
|
|
120
|
+
- `markAsDirty()` / `markAsPristine()`: Tracks if the value has been modified.
|
|
121
|
+
- `updateValueAndValidity()`: Manually triggers recalculation of value and status.
|
|
122
|
+
- Options `{ emitEvent: false }` or `{ onlySelf: true }` can be passed to most methods to control propagation.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Rendering Strategies
|
|
2
|
+
|
|
3
|
+
Angular supports multiple rendering strategies to optimize for SEO, performance, and interactivity.
|
|
4
|
+
|
|
5
|
+
## 1. Client-Side Rendering (CSR)
|
|
6
|
+
|
|
7
|
+
**Default Strategy.** Content is rendered entirely in the browser.
|
|
8
|
+
|
|
9
|
+
- **Use case**: Interactive dashboards, internal tools.
|
|
10
|
+
- **Pros**: Simplest to configure, low server cost.
|
|
11
|
+
- **Cons**: Poor SEO, slower initial content visibility (must wait for JS).
|
|
12
|
+
|
|
13
|
+
## 2. Static Site Generation (SSG / Prerendering)
|
|
14
|
+
|
|
15
|
+
Content is pre-rendered into static HTML files at **build time**.
|
|
16
|
+
|
|
17
|
+
- **Use case**: Marketing pages, blogs, documentation.
|
|
18
|
+
- **Pros**: Fastest initial load, excellent SEO, CDN-friendly.
|
|
19
|
+
- **Cons**: Requires rebuild for content updates, not for user-specific data.
|
|
20
|
+
|
|
21
|
+
## 3. Server-Side Rendering (SSR)
|
|
22
|
+
|
|
23
|
+
Content is rendered on the server for the **initial request**. Subsequent navigations happen client-side (SPA style).
|
|
24
|
+
|
|
25
|
+
- **Use case**: E-commerce product pages, news sites, personalized dynamic content.
|
|
26
|
+
- **Pros**: Excellent SEO, fast initial content visibility.
|
|
27
|
+
- **Cons**: Requires a server (Node.js), higher server cost/latency.
|
|
28
|
+
|
|
29
|
+
## Hydration
|
|
30
|
+
|
|
31
|
+
Hydration is the process of making server-rendered HTML interactive in the browser.
|
|
32
|
+
|
|
33
|
+
- **Full Hydration**: The entire app becomes interactive at once.
|
|
34
|
+
- **Incremental Hydration**: (Advanced) Parts become interactive as needed using `@defer` blocks.
|
|
35
|
+
- **Event Replay**: Captures and replays user events that happened before hydration finished.
|
|
36
|
+
|
|
37
|
+
## Decision Matrix
|
|
38
|
+
|
|
39
|
+
| Requirement | Strategy |
|
|
40
|
+
| :------------------------------ | :------------------- |
|
|
41
|
+
| **SEO + Static Content** | SSG |
|
|
42
|
+
| **SEO + Dynamic Content** | SSR |
|
|
43
|
+
| **No SEO + High Interactivity** | CSR |
|
|
44
|
+
| **Mixed** | Hybrid (Route-based) |
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Async Reactivity with `resource`
|
|
2
|
+
|
|
3
|
+
> [!IMPORTANT]
|
|
4
|
+
> The `resource` API is currently experimental in Angular.
|
|
5
|
+
|
|
6
|
+
A `Resource` incorporates asynchronous data fetching into Angular's signal-based reactivity. It executes an async loader function whenever its dependencies change, exposing the status and result as synchronous signals.
|
|
7
|
+
|
|
8
|
+
## Basic Usage
|
|
9
|
+
|
|
10
|
+
The `resource` function accepts an options object with two main properties:
|
|
11
|
+
|
|
12
|
+
1. `params`: A reactive computation (like `computed`). When signals read here change, the resource re-fetches.
|
|
13
|
+
2. `loader`: An async function that fetches data based on the parameters.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { Component, resource, signal, computed } from '@angular/core';
|
|
17
|
+
|
|
18
|
+
@Component({...})
|
|
19
|
+
export class UserProfile {
|
|
20
|
+
userId = signal('123');
|
|
21
|
+
|
|
22
|
+
userResource = resource({
|
|
23
|
+
// Reactively tracking userId
|
|
24
|
+
params: () => ({ id: this.userId() }),
|
|
25
|
+
|
|
26
|
+
// Executes whenever params change
|
|
27
|
+
loader: async ({ params, abortSignal }) => {
|
|
28
|
+
const response = await fetch(`/api/users/${params.id}`, { signal: abortSignal });
|
|
29
|
+
if (!response.ok) throw new Error('Network error');
|
|
30
|
+
return response.json();
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Use the resource value in computed signals
|
|
35
|
+
userName = computed(() => {
|
|
36
|
+
if (this.userResource.hasValue()) {
|
|
37
|
+
return this.userResource.value()?.name;
|
|
38
|
+
} else {
|
|
39
|
+
return 'Loading...';
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Aborting Requests
|
|
46
|
+
|
|
47
|
+
If the `params` signal changes while a previous loader is still running, the `Resource` will attempt to abort the outstanding request using the provided `abortSignal`. **Always pass `abortSignal` to your `fetch` calls.**
|
|
48
|
+
|
|
49
|
+
## Reloading Data
|
|
50
|
+
|
|
51
|
+
You can imperatively force the resource to re-run the loader without the params changing by calling `.reload()`.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
this.userResource.reload();
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Resource Status Signals
|
|
58
|
+
|
|
59
|
+
The `Resource` object provides several signals to read its current state:
|
|
60
|
+
|
|
61
|
+
- `value()`: The resolved data, or `undefined`.
|
|
62
|
+
- `hasValue()`: Type-guard boolean. `true` if a value exists.
|
|
63
|
+
- `isLoading()`: Boolean indicating if the loader is currently running.
|
|
64
|
+
- `error()`: The error thrown by the loader, or `undefined`.
|
|
65
|
+
- `status()`: A string constant representing the exact state (`'idle'`, `'loading'`, `'resolved'`, `'error'`, `'reloading'`, `'local'`).
|
|
66
|
+
|
|
67
|
+
## Local Mutation
|
|
68
|
+
|
|
69
|
+
You can optimistically update the resource's value directly. This changes the status to `'local'`.
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
this.userResource.value.set({name: 'Optimistic Update'});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Reactive Data Fetching with `httpResource`
|
|
76
|
+
|
|
77
|
+
If you are using Angular's `HttpClient`, prefer using `httpResource`. It is a specialized wrapper that leverages the Angular HTTP stack (including interceptors) while providing the same signal-based resource API.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Route Transition Animations
|
|
2
|
+
|
|
3
|
+
Angular Router supports the browser's **View Transitions API** for smooth visual transitions between routes.
|
|
4
|
+
|
|
5
|
+
## Enabling View Transitions
|
|
6
|
+
|
|
7
|
+
Add `withViewTransitions()` to your router configuration.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
provideRouter(routes, withViewTransitions());
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
This is a **progressive enhancement**. In browsers that don't support the API, the router will still work but without the transition animation.
|
|
14
|
+
|
|
15
|
+
## How it Works
|
|
16
|
+
|
|
17
|
+
1. Browser takes a screenshot of the old state.
|
|
18
|
+
2. Router updates the DOM (activates new component).
|
|
19
|
+
3. Browser takes a screenshot of the new state.
|
|
20
|
+
4. Browser animates between the two states.
|
|
21
|
+
|
|
22
|
+
## Customizing with CSS
|
|
23
|
+
|
|
24
|
+
Transitions are customized in **global CSS files** (not component-scoped CSS).
|
|
25
|
+
|
|
26
|
+
Use the `::view-transition-old()` and `::view-transition-new()` pseudo-elements.
|
|
27
|
+
|
|
28
|
+
```css
|
|
29
|
+
/* Example: Cross-fade + Slide */
|
|
30
|
+
::view-transition-old(root) {
|
|
31
|
+
animation: 90ms cubic-bezier(0.4, 0, 1, 1) both fade-out;
|
|
32
|
+
}
|
|
33
|
+
::view-transition-new(root) {
|
|
34
|
+
animation: 210ms cubic-bezier(0, 0, 0.2, 1) 90ms both fade-in;
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Advanced Control
|
|
39
|
+
|
|
40
|
+
Use `onViewTransitionCreated` to skip transitions or customize behavior based on the navigation context.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
withViewTransitions({
|
|
44
|
+
onViewTransitionCreated: ({transition, from, to}) => {
|
|
45
|
+
// Skip animation for specific routes
|
|
46
|
+
if (to.url === '/no-animation') {
|
|
47
|
+
transition.skipTransition();
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Best Practices
|
|
54
|
+
|
|
55
|
+
- **Global Styles**: Always define transition animations in `styles.css` to avoid view encapsulation issues.
|
|
56
|
+
- **View Transition Names**: Assign unique `view-transition-name` to elements that should transition smoothly across routes (e.g., a header image).
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Route Guards
|
|
2
|
+
|
|
3
|
+
Route guards control whether a user can navigate to or leave a route.
|
|
4
|
+
|
|
5
|
+
## Types of Guards
|
|
6
|
+
|
|
7
|
+
- **`CanActivate`**: Can the user access this route? (e.g., Auth check).
|
|
8
|
+
- **`CanActivateChild`**: Can the user access children of this route?
|
|
9
|
+
- **`CanDeactivate`**: Can the user leave this route? (e.g., Unsaved changes).
|
|
10
|
+
- **`CanMatch`**: Should this route even be considered for matching? (e.g., Feature flags). If it returns `false`, the router continues checking other routes.
|
|
11
|
+
|
|
12
|
+
## Creating a Guard
|
|
13
|
+
|
|
14
|
+
Guards are typically functional since Angular 15.
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
export const authGuard: CanActivateFn = (route, state) => {
|
|
18
|
+
const authService = inject(AuthService);
|
|
19
|
+
const router = inject(Router);
|
|
20
|
+
|
|
21
|
+
if (authService.isLoggedIn()) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Redirect to login
|
|
26
|
+
return router.parseUrl('/login');
|
|
27
|
+
};
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Applying Guards
|
|
31
|
+
|
|
32
|
+
Add them to the route configuration as an array. They execute in order.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
{
|
|
36
|
+
path: 'admin',
|
|
37
|
+
component: Admin,
|
|
38
|
+
canActivate: [authGuard],
|
|
39
|
+
canActivateChild: [adminChildGuard],
|
|
40
|
+
canDeactivate: [unsavedChangesGuard]
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Return Values
|
|
45
|
+
|
|
46
|
+
- `boolean`: `true` to allow, `false` to block.
|
|
47
|
+
- `UrlTree` or `RedirectCommand`: Redirect to a different route.
|
|
48
|
+
- `Observable` or `Promise`: Resolves to the above types.
|
|
49
|
+
|
|
50
|
+
## Security Note
|
|
51
|
+
|
|
52
|
+
**Client-side guards are NOT a substitute for server-side security.** Always verify permissions on the server.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Router Lifecycle and Events
|
|
2
|
+
|
|
3
|
+
Angular Router emits events through the `Router.events` observable, allowing you to track the navigation lifecycle from start to finish.
|
|
4
|
+
|
|
5
|
+
## Common Router Events (Chronological)
|
|
6
|
+
|
|
7
|
+
1. **`NavigationStart`**: Navigation begins.
|
|
8
|
+
2. **`RoutesRecognized`**: Router matches the URL to a route.
|
|
9
|
+
3. **`GuardsCheckStart` / `End`**: Evaluation of `canActivate`, `canMatch`, etc.
|
|
10
|
+
4. **`ResolveStart` / `End`**: Data resolution phase (fetching data via resolvers).
|
|
11
|
+
5. **`NavigationEnd`**: Navigation completed successfully.
|
|
12
|
+
6. **`NavigationCancel`**: Navigation canceled (e.g., guard returned `false`).
|
|
13
|
+
7. **`NavigationError`**: Navigation failed (e.g., error in resolver).
|
|
14
|
+
|
|
15
|
+
## Subscribing to Events
|
|
16
|
+
|
|
17
|
+
Inject the `Router` and filter the `events` observable.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import {Router, NavigationStart, NavigationEnd} from '@angular/router';
|
|
21
|
+
|
|
22
|
+
export class MyService {
|
|
23
|
+
private router = inject(Router);
|
|
24
|
+
|
|
25
|
+
constructor() {
|
|
26
|
+
this.router.events.pipe(filter((e) => e instanceof NavigationEnd)).subscribe((event) => {
|
|
27
|
+
console.log('Navigated to:', event.url);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Debugging
|
|
34
|
+
|
|
35
|
+
Enable detailed console logging of all routing events during application bootstrap.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
provideRouter(routes, withDebugTracing());
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Common Use Cases
|
|
42
|
+
|
|
43
|
+
- **Loading Indicators**: Show a spinner when `NavigationStart` fires and hide it on `NavigationEnd`/`Cancel`/`Error`.
|
|
44
|
+
- **Analytics**: Track page views by listening for `NavigationEnd`.
|
|
45
|
+
- **Scroll Management**: Respond to `Scroll` events for custom scroll behavior.
|