Skip to content

Commit 5021143

Browse files
committed
fix(migrate): md-class-c src/content/guide/directives
1 parent d531132 commit 5021143

2 files changed

Lines changed: 55 additions & 77 deletions

File tree

‎adev-ja/src/content/guide/directives/structural-directives.en.md‎

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
Structural directives are directives applied to an `<ng-template>` element that conditionally or repeatedly render the content of that `<ng-template>`.
44

5+
For everyday conditional and repeated rendering, use Angular's built-in [control flow blocks](guide/templates/control-flow) (`@if`, `@for`, and `@switch`). Write a structural directive when you need reusable rendering behavior that control flow doesn't cover, such as gating content behind a permission check or providing a template with data from an external source.
6+
57
## Example use case
68

7-
In this guide you'll build a structural directive which fetches data from a given data source and renders its template when that data is available. This directive is called `SelectDirective`, after the SQL keyword `SELECT`, and match it with an attribute selector `[select]`.
9+
This guide uses a directive called `SelectDirective` as its running example. The directive fetches data from a given data source and renders its template when that data is available. It is named after the SQL keyword `SELECT` and matched with an attribute selector, `[select]`.
810

9-
`SelectDirective` will have an input naming the data source to be used, which you will call `selectFrom`. The `select` prefix for this input is important for the [shorthand syntax](#structural-directive-shorthand). The directive will instantiate its `<ng-template>` with a template context providing the selected data.
11+
`SelectDirective` has an input naming the data source to use, called `selectFrom`. The `select` prefix for this input is important for the [shorthand syntax](#structural-directive-shorthand). The directive instantiates its `<ng-template>` with a template context providing the selected data.
1012

11-
The following is an example of using this directive directly on an `<ng-template>` would look like:
13+
Using this directive directly on an `<ng-template>` looks like this:
1214

1315
```angular-html
1416
<ng-template select let-data [selectFrom]="source">
@@ -18,7 +20,7 @@ The following is an example of using this directive directly on an `<ng-template
1820

1921
The structural directive can wait for the data to become available and then render its `<ng-template>`.
2022

21-
HELPFUL: Note that Angular's `<ng-template>` element defines a template that doesn't render anything by default, if you just wrap elements in an `<ng-template>` without applying a structural directive those elements will not be rendered.
23+
HELPFUL: Angular's `<ng-template>` element defines a template that doesn't render anything by default. If you wrap elements in an `<ng-template>` without applying a structural directive, those elements are not rendered.
2224

2325
For more information, see the [ng-template API](api/core/ng-template) documentation.
2426

@@ -60,20 +62,12 @@ You can only apply one structural directive per element when using the shorthand
6062

6163
## Creating a structural directive
6264

63-
This section guides you through creating the `SelectDirective`.
64-
65-
<docs-workflow>
66-
<docs-step title="Generate the directive">
67-
Using the Angular CLI, run the following command, where `select` is the name of the directive:
65+
A structural directive is a directive class that injects two dependencies:
6866

69-
```shell
70-
ng generate directive select
71-
```
67+
- [`TemplateRef`](api/core/TemplateRef) gives the directive access to the content of the `<ng-template>` it is applied to.
68+
- [`ViewContainerRef`](api/core/ViewContainerRef) represents the location in the DOM where the directive can render that template.
7269

73-
Angular creates the directive class and specifies the CSS selector, `[select]`, that identifies the directive in a template.
74-
</docs-step>
75-
<docs-step title="Make the directive structural">
76-
Import `TemplateRef`, `ViewContainerRef`, and `input`. Inject `TemplateRef` and `ViewContainerRef` in the directive as private properties.
70+
The directive controls rendering by creating, or not creating, embedded views from the template in the view container. The complete `SelectDirective` looks like this:
7771

7872
```ts
7973
import {Directive, TemplateRef, ViewContainerRef, inject, input} from '@angular/core';
@@ -88,27 +82,9 @@ export interface DataSource<T> {
8882
export class SelectDirective {
8983
private templateRef = inject(TemplateRef);
9084
private viewContainerRef = inject(ViewContainerRef);
91-
}
92-
```
9385

94-
</docs-step>
95-
<docs-step title="Add the 'selectFrom' input">
96-
Add a `selectFrom` `input()` property.
97-
98-
```ts
99-
export class SelectDirective {
100-
// ...
10186
selectFrom = input.required<DataSource<unknown>>();
102-
}
103-
```
104-
105-
</docs-step>
106-
<docs-step title="Add the business logic">
107-
With `SelectDirective` now scaffolded as a structural directive with its input, you can now add the logic to fetch the data and render the template with it:
10887

109-
```ts
110-
export class SelectDirective {
111-
// ...
11288
async ngOnInit() {
11389
const data = await this.selectFrom().load();
11490
this.viewContainerRef.createEmbeddedView(this.templateRef, {
@@ -120,10 +96,15 @@ export class SelectDirective {
12096
}
12197
```
12298

123-
</docs-step>
124-
</docs-workflow>
99+
The `selectFrom` input names the data source the directive reads from. It uses [`input.required()`](guide/components/inputs#required-inputs) because the directive can't do anything useful without a data source.
100+
101+
When Angular initializes the directive, it loads the data and then renders the template by calling `createEmbeddedView()`. The second argument is the template's _context object_: values the template can bind to with `let` declarations. Assigning the data to the `$implicit` key makes it the default value that `let-data` (or `let data` in shorthand) receives.
125102

126-
That's it - `SelectDirective` is up and running. A follow-up step might be to [add template type-checking support](#typing-the-directives-context).
103+
NOTE: This example renders its template once, when the directive initializes. It does not re-render when the bound data source changes.
104+
105+
Once the directive works, consider [adding template type-checking support](#typing-the-directives-context).
106+
107+
HELPFUL: The CLI command [`ng generate directive`](tools/cli/schematics) scaffolds a directive along with its test file.
127108

128109
## Structural directive syntax reference
129110

@@ -167,8 +148,8 @@ The following table provides shorthand examples:
167148
| :-------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------ |
168149
| `*myDir="let item of [1,2,3]"` | `<ng-template myDir let-item [myDirOf]="[1, 2, 3]">` |
169150
| `*myDir="let item of [1,2,3] as items; trackBy: myTrack; index as i"` | `<ng-template myDir let-item [myDirOf]="[1,2,3]" let-items="myDirOf" [myDirTrackBy]="myTrack" let-i="index">` |
170-
| `*ngComponentOutlet="componentClass";` | `<ng-template [ngComponentOutlet]="componentClass">` |
171-
| `*ngComponentOutlet="componentClass; inputs: myInputs";` | `<ng-template [ngComponentOutlet]="componentClass" [ngComponentOutletInputs]="myInputs">` |
151+
| `*ngComponentOutlet="componentClass"` | `<ng-template [ngComponentOutlet]="componentClass">` |
152+
| `*ngComponentOutlet="componentClass; inputs: myInputs"` | `<ng-template [ngComponentOutlet]="componentClass" [ngComponentOutletInputs]="myInputs">` |
172153
| `*myDir="exp as value"` | `<ng-template [myDir]="exp" let-value="myDir">` |
173154
174155
## Improving template type checking for custom directives
@@ -251,3 +232,11 @@ export class SelectDirective<T> {
251232
}
252233
}
253234
```
235+
236+
## What's next
237+
238+
<docs-pill-row>
239+
<docs-pill href="guide/directives/directive-composition-api" title="Directive composition API"/>
240+
<docs-pill href="guide/templates/ng-template" title="ng-template"/>
241+
<docs-pill href="guide/templates/control-flow" title="Control flow"/>
242+
</docs-pill-row>

‎adev-ja/src/content/guide/directives/structural-directives.md‎

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
構造ディレクティブは、`<ng-template>`要素に適用され、その`<ng-template>`の内容を条件付きまたは繰り返しでレンダリングするディレクティブです。
44

5+
日常的な条件付きレンダリングや繰り返しレンダリングには、Angular組み込みの[制御フローブロック](guide/templates/control-flow)(`@if`、`@for`、`@switch`)を使用してください。権限チェックによってコンテンツの表示を制限したり、外部ソースからのデータをテンプレートに提供したりするなど、制御フローでは対応できない再利用可能なレンダリング動作が必要な場合に、構造ディレクティブを作成します。
6+
57
## 使用例
68

7-
このガイドでは、指定されたデータソースからデータを取得し、そのデータが利用可能になったときにテンプレートをレンダリングする構造ディレクティブを作成します。このディレクティブは、SQLキーワード`SELECT`にちなんで`SelectDirective`と呼ばれ、属性セレクター`[select]`と一致させます。
9+
このガイドでは、`SelectDirective`という名前のディレクティブを実例として使用します。このディレクティブは、指定されたデータソースからデータを取得し、そのデータが利用可能になったときにテンプレートをレンダリングします。SQLキーワード`SELECT`にちなんで名付けられ、属性セレクター`[select]`と一致させます。
810

9-
`SelectDirective`には、使用するデータソースの名前を指定する入力があり、`selectFrom`と呼びます。この入力の`select`プレフィックスは、[省略記法](#structural-directive-shorthand)にとって重要です。ディレクティブは、選択されたデータを提供するテンプレートコンテキストを使用して、`<ng-template>`をインスタンス化します。
11+
`SelectDirective`には、使用するデータソースの名前を指定する`selectFrom`という入力があります。この入力の`select`プレフィックスは、[省略記法](#structural-directive-shorthand)にとって重要です。ディレクティブは、選択されたデータを提供するテンプレートコンテキストを使用して、`<ng-template>`をインスタンス化します。
1012

11-
以下は、`<ng-template>`に直接このディレクティブを適用した例です。
13+
このディレクティブを`<ng-template>`に直接使用すると、次のようになります。
1214

1315
```angular-html
1416
<ng-template select let-data [selectFrom]="source">
@@ -60,20 +62,12 @@ Angularは、構造ディレクティブの省略記法をサポートしてお
6062

6163
## 構造ディレクティブの作成
6264

63-
このセクションでは、`SelectDirective`の作成について説明します。
64-
65-
<docs-workflow>
66-
<docs-step title="ディレクティブを生成">
67-
Angular CLIを使用して、次のコマンドを実行します。ここで、`select`はディレクティブの名前です。
65+
構造ディレクティブは、2つの依存関係を注入するディレクティブクラスです。
6866

69-
```shell
70-
ng generate directive select
71-
```
67+
- [`TemplateRef`](api/core/TemplateRef)は、ディレクティブが適用されている`<ng-template>`の内容へのアクセスを提供します。
68+
- [`ViewContainerRef`](api/core/ViewContainerRef)は、ディレクティブがそのテンプレートをレンダリングできるDOM上の位置を表します。
7269

73-
Angularは、ディレクティブクラスを作成し、テンプレートでディレクティブを識別するCSSセレクター`[select]`を指定します。
74-
</docs-step>
75-
<docs-step title="ディレクティブを構造化">
76-
`TemplateRef`、`ViewContainerRef`、`input`をインポートし、ディレクティブ内で`TemplateRef`と`ViewContainerRef`をプライベートプロパティとしてインジェクトします。
70+
ディレクティブは、ビューコンテナ内のテンプレートから埋め込みビューを作成する、または作成しないことでレンダリングを制御します。完成した`SelectDirective`は次のようになります。
7771

7872
```ts
7973
import {Directive, TemplateRef, ViewContainerRef, inject, input} from '@angular/core';
@@ -88,27 +82,9 @@ export interface DataSource<T> {
8882
export class SelectDirective {
8983
private templateRef = inject(TemplateRef);
9084
private viewContainerRef = inject(ViewContainerRef);
91-
}
92-
```
9385

94-
</docs-step>
95-
<docs-step title="'selectFrom'入力を追加">
96-
`selectFrom` `input()`プロパティを追加します。
97-
98-
```ts
99-
export class SelectDirective {
100-
// ...
10186
selectFrom = input.required<DataSource<unknown>>();
102-
}
103-
```
104-
105-
</docs-step>
106-
<docs-step title="ビジネスロジックを追加">
107-
`SelectDirective`が、入力を持つ構造ディレクティブとしてスキャフォールディングされたので、データを取得し、テンプレートをデータと共にレンダリングするロジックを追加できます。
10887

109-
```ts
110-
export class SelectDirective {
111-
// ...
11288
async ngOnInit() {
11389
const data = await this.selectFrom().load();
11490
this.viewContainerRef.createEmbeddedView(this.templateRef, {
@@ -120,10 +96,15 @@ export class SelectDirective {
12096
}
12197
```
12298

123-
</docs-step>
124-
</docs-workflow>
99+
`selectFrom`入力は、ディレクティブが読み取るデータソースの名前を指定します。ディレクティブはデータソースなしでは何も役に立たないため、[`input.required()`](guide/components/inputs#required-inputs)を使用しています。
100+
101+
Angularがディレクティブを初期化すると、データを読み込んでから`createEmbeddedView()`を呼び出してテンプレートをレンダリングします。2番目の引数はテンプレートの*コンテキストオブジェクト*であり、テンプレートが`let`宣言でバインドできる値です。データを`$implicit`キーに割り当てることで、`let-data`(または省略記法での`let data`)が受け取るデフォルト値になります。
125102

126-
これで`SelectDirective`は起動して実行できるようになりました。次のステップとして、[テンプレートタイプチェックのサポートを追加](#typing-the-directives-context)できます。
103+
NOTE: この例では、ディレクティブが初期化されるときに一度だけテンプレートをレンダリングします。バインドされたデータソースが変更されても再レンダリングされません。
104+
105+
ディレクティブが動作するようになったら、[テンプレートタイプチェックのサポートを追加](#typing-the-directives-context)することを検討してください。
106+
107+
HELPFUL: CLIコマンド[`ng generate directive`](tools/cli/schematics)は、テストファイルとともにディレクティブをスキャフォールディングします。
127108

128109
## 構造ディレクティブの構文リファレンス {#structural-directive-syntax-reference}
129110

@@ -167,8 +148,8 @@ Angularは、構造ディレクティブの省略記法を次の通常のバイ
167148
| :-------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------ |
168149
| `*myDir="let item of [1,2,3]"` | `<ng-template myDir let-item [myDirOf]="[1, 2, 3]">` |
169150
| `*myDir="let item of [1,2,3] as items; trackBy: myTrack; index as i"` | `<ng-template myDir let-item [myDirOf]="[1,2,3]" let-items="myDirOf" [myDirTrackBy]="myTrack" let-i="index">` |
170-
| `*ngComponentOutlet="componentClass";` | `<ng-template [ngComponentOutlet]="componentClass">` |
171-
| `*ngComponentOutlet="componentClass; inputs: myInputs";` | `<ng-template [ngComponentOutlet]="componentClass" [ngComponentOutletInputs]="myInputs">` |
151+
| `*ngComponentOutlet="componentClass"` | `<ng-template [ngComponentOutlet]="componentClass">` |
152+
| `*ngComponentOutlet="componentClass; inputs: myInputs"` | `<ng-template [ngComponentOutlet]="componentClass" [ngComponentOutletInputs]="myInputs">` |
172153
| `*myDir="exp as value"` | `<ng-template [myDir]="exp" let-value="myDir">` |
173154
174155
## カスタムディレクティブのテンプレートタイプチェックを改善する {#improving-template-type-checking-for-custom-directives}
@@ -251,3 +232,11 @@ export class SelectDirective<T> {
251232
}
252233
}
253234
```
235+
236+
## What's next
237+
238+
<docs-pill-row>
239+
<docs-pill href="guide/directives/directive-composition-api" title="Directive composition API"/>
240+
<docs-pill href="guide/templates/ng-template" title="ng-template"/>
241+
<docs-pill href="guide/templates/control-flow" title="Control flow"/>
242+
</docs-pill-row>

0 commit comments

Comments
 (0)