Skip to content

Commit 6b3f0bd

Browse files
pkozlowski-opensourcealxhub
authored andcommitted
docs: control flow API reference (angular#52397)
Adds API reference for the new built-in control flow. PR Close angular#52397
1 parent 9291ffc commit 6b3f0bd

File tree

3 files changed

+88
-6
lines changed

3 files changed

+88
-6
lines changed

aio/content/blocks/core/for.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
1-
Placeholder content
1+
The `@for` block repeatedly renders content of a block for each item in a collection.
2+
3+
@syntax
4+
5+
```html
6+
@for (item of items; track item.name) {
7+
<li> {{ item.name }} </li>
8+
} @empty {
9+
<li> There are no items. </li>
10+
}
11+
```
12+
13+
@description
14+
15+
The `@for` block renders its content in response to changes in a collection. Collections can be any JavaScript [iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols), but there are performance advantages of using a regular `Array`.
16+
17+
You can optionally include an `@empty` section immediately after the `@for` block content. The content of the `@empty` block displays when there are no items.
18+
19+
<h3> track and objects identity </h3>
20+
21+
The value of the `track` expression determines a key used to associate array items with the views in the DOM. Having clear indication of the item identity allows Angular to execute a minimal set of DOM operations as items are added, removed or moved in a collection.
22+
23+
Loops over immutable data without `trackBy` as one of the most common causes for performance issues across Angular applications. Because of the potential for poor performance, the `track` expression is required for the `@for` loops. When in doubt, using `track $index` is a good default.
24+
25+
<h3> `$index` and other contextual variables </h3>
26+
27+
Inside `@for` contents, several implicit variables are always available:
28+
29+
| Variable | Meaning |
30+
| -------- | ------- |
31+
| `$count` | Number of items in a collection iterated over |
32+
| `$index` | Index of the current row |
33+
| `$first` | Whether the current row is the first row |
34+
| `$last` | Whether the current row is the last row |
35+
| `$even` | Whether the current row index is even |
36+
| `$odd` | Whether the current row index is odd |
37+
38+
These variables are always available with these names, but can be aliased via a `let` segment:
39+
40+
```html
41+
@for (item of items; track item.id; let idx = $index, e = $even) {
42+
Item #{{ idx }}: {{ item.name }}
43+
}
44+
```
45+
46+
The aliasing is especially useful in case of using nested `@for` blocks where contextual variable names could collide.

aio/content/blocks/core/if.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1-
Control flow block used to conditionally render content in the DOM.
1+
The `@if` block conditionally displays its content when its condition expression is truthy.
22

33
@syntax
44

55
```html
6-
@if ( <condition> ) {
7-
<!-- conditional template fragment -->
6+
@if (a > b) {
7+
{{a}} is greater than {{b}}
8+
} @else if (b > a) {
9+
{{a}} is less than {{b}}
10+
} @else {
11+
{{a}} is equal to {{b}}
812
}
913
```
1014

1115
@description
12-
This is the full description.
16+
17+
Content is added and removed from the DOM based on the evaluation of conditional expressions in the `@if` and `@else` blocks.
18+
19+
The built-in `@if` supports referencing of expression results to keep a solution for common coding patterns:
20+
21+
```html
22+
@if (users$ | async; as users) {
23+
{{ users.length }}
24+
}
25+
```

aio/content/blocks/core/switch.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
1-
Placeholder content
1+
The `@switch` block is inspired by the JavaScript `switch` statement:
2+
3+
@syntax
4+
5+
```html
6+
@switch (condition) {
7+
@case (caseA) {
8+
Case A.
9+
}
10+
@case (caseB) {
11+
Case B.
12+
}
13+
@default {
14+
Default case.
15+
}
16+
}
17+
```
18+
19+
@description
20+
21+
The `@switch` blocks displays content selected by one of the cases matching against the conditional expression. The value of the conditional expression is compared to the case expression using the `===` operator.
22+
23+
The `@default` block is optional and can be omitted. If no `@case` matches the expression and there is no `@default` block, nothing is shown.
24+
25+
**`@switch` does not have fallthrough**, so you do not need an equivalent to a `break` or `return` statement.

0 commit comments

Comments
 (0)