配列から動的に作成するコンポーネントへの参照を作成する方法がどこにも見つかりませんか?
HTML
<div *ngFor="let item of items">
<my-component [data]="item.data"></my-component>
</div>
<my-component>
TypeScriptコード内のすべて(配列から動的に作成されます)にアクセスする必要があります
例:
配列は、それではtypescriptですコードで、私はこのような何かを持っている必要があります2です。length
items
myComponent0-1への参照。 <my-component>
myComponent1-2への参照。 <my-component>
ありがとう
使用する ViewChildren
import { ViewChildren, QueryList, ... } from '@angular/core';
export class AppComponent {
@ViewChildren(MyComponent) components: QueryList<MyComponent>
...
ngAfterViewInit() {
this.components.forEach(componentInstance => console.log(componentInstance));
}
...
}
これMyComponent
は、コンポーネントクラスのエクスポートされた名前です。これは一般的にタイプであるQueryList
ため、基本的に配列として扱い、必要なことを行うことができます。
これはAfterViewInit
、コンポーネントのフックの後でのみ使用できます。
ここだサンプルStackBlitzご参照のために。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加