Skip to content

Commit 4662878

Browse files
committed
refactor(@angular/cli): update MCP example tool format for if example
The sample `@if` example for the MCP experimental `find_examples` tool has been updated to use a more structured format. This format is being used to evaluate optimal structure for future examples.
1 parent 301b50d commit 4662878

File tree

1 file changed

+74
-15
lines changed

1 file changed

+74
-15
lines changed
Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,87 @@
1-
# Angular @if Control Flow Example
1+
---
2+
title: 'Using the @if Built-in Control Flow Block'
3+
summary: 'Demonstrates how to use the @if built-in control flow block to conditionally render content in an Angular template based on a boolean expression.'
4+
keywords:
5+
- '@if'
6+
- 'control flow'
7+
- 'conditional rendering'
8+
- 'template syntax'
9+
related_concepts:
10+
- '@else'
11+
- '@else if'
12+
- 'signals'
13+
related_tools:
14+
- 'modernize'
15+
---
216

3-
This example demonstrates how to use the `@if` control flow block in an Angular template. The visibility of a `<div>` element is controlled by a boolean field in the component's TypeScript code.
17+
## Purpose
418

5-
## Angular Template
19+
The purpose of this pattern is to create dynamic user interfaces by controlling which elements are rendered to the DOM based on the application's state. This is a fundamental technique for building responsive and interactive components.
620

7-
```html
8-
<!-- The @if directive will only render this div if the 'isVisible' signal in the component is true. -->
9-
@if (isVisible()) {
10-
<div>This content is conditionally displayed.</div>
21+
## When to Use
22+
23+
Use the `@if` block as the modern, preferred alternative to the `*ngIf` directive for all conditional rendering. It offers better type-checking and a cleaner, more intuitive syntax within the template.
24+
25+
## Key Concepts
26+
27+
- **`@if` block:** The primary syntax for conditional rendering in modern Angular templates. It evaluates a boolean expression and renders the content within its block if the expression is true.
28+
29+
## Example Files
30+
31+
### `conditional-content.component.ts`
32+
33+
This is a self-contained standalone component that demonstrates the `@if` block with an optional `@else` block.
34+
35+
```typescript
36+
import { Component, signal } from '@angular/core';
37+
38+
@Component({
39+
selector: 'app-conditional-content',
40+
standalone: true,
41+
template: `
42+
<button (click)="toggleVisibility()">Toggle Content</button>
43+
44+
@if (isVisible()) {
45+
<div>This content is conditionally displayed.</div>
46+
} @else {
47+
<div>The content is hidden. Click the button to show it.</div>
48+
}
49+
`,
50+
})
51+
export class ConditionalContentComponent {
52+
protected readonly isVisible = signal(true);
53+
54+
toggleVisibility(): void {
55+
this.isVisible.update((v) => !v);
56+
}
1157
}
1258
```
1359

14-
## Component TypeScript
60+
## Usage Notes
61+
62+
- The expression inside the `@if ()` block must evaluate to a boolean.
63+
- This example uses a signal, which is a common pattern, but any boolean property or method call from the component can be used.
64+
- The `@else` block is optional and is rendered when the `@if` condition is `false`.
65+
66+
## How to Use This Example
67+
68+
### 1. Import the Component
69+
70+
In a standalone architecture, import the component into the `imports` array of the parent component where you want to use it.
1571

1672
```typescript
73+
// in app.component.ts
1774
import { Component } from '@angular/core';
75+
import { ConditionalContentComponent } from './conditional-content.component';
1876

1977
@Component({
20-
selector: 'app-example',
21-
templateUrl: './example.html',
22-
styleUrl: './example.css',
78+
selector: 'app-root',
79+
standalone: true,
80+
imports: [ConditionalContentComponent],
81+
template: `
82+
<h1>My Application</h1>
83+
<app-conditional-content></app-conditional-content>
84+
`,
2385
})
24-
export class Example {
25-
// This boolean signal controls the visibility of the element in the template.
26-
protected readonly isVisible = signal(true);
27-
}
86+
export class AppComponent {}
2887
```

0 commit comments

Comments
 (0)