Javascript 如何在angular 4中指定动态零部件参考

Javascript 如何在angular 4中指定动态零部件参考,javascript,angular,Javascript,Angular,给定一个组件,我们如何动态分配组件引用,如下所示: 而不是 假设您在NGF中有您的列表审核: 您可以将@ViewChildren与组件选择器一起使用: export class MyComoponent { @ViewChildren(ListAuditComponent) listAudits: QueryList<ListAuditComponent>; } 导出类mycoponent{ @ViewChildren(ListAuditComponent)listAu

给定一个组件,我们如何动态分配组件引用,如下所示:

而不是


假设您在NGF中有您的列表审核:

您可以将@ViewChildren与组件选择器一起使用:

export class MyComoponent {
   @ViewChildren(ListAuditComponent) listAudits: QueryList<ListAuditComponent>;
}
导出类mycoponent{
@ViewChildren(ListAuditComponent)listAudits:QueryList;
}

我不会那样使用组件的引用。我可以使用querylist引用它们,如下所示:

import { Component, QueryList, ViewChildren } from '@angular/core';
import { AppListComponent } from './applist.component';

@Component({
    selector: 'app-root',
    template: `
    <app-list-audit></app-list-audit>
    <app-list-audit></app-list-audit>
    <app-list-audit></app-list-audit> `
})
export class AppComponent {
  @ViewChildren(AppListComponent) list: QueryList<AppListComponent>;
  compArr = [];

  ngAfterViewInit() {
    this.compArr = this.list.toArray();
  }
}
从'@angular/core'导入{Component,QueryList,ViewChildren};
从“./applist.component”导入{AppListComponent};
@组成部分({
选择器:'应用程序根',
模板:`
`
})
导出类AppComponent{
@ViewChildren(AppListComponent)列表:QueryList;
比较器=[];
ngAfterViewInit(){
this.compArr=this.list.toArray();
}
}

然后,您可以使用compArr来引用模板中的所有组件实例。

应该是这样。问题是什么?我如何检查引用是否已设置,因为当我使用类似于
@viewchild('LAref'+this.Identifier)的viewchild时,FLADT:Component它显示为未定义。您需要使用viewchildren:)请解释如何解释为组件分配引用的最终目标是什么?您不能只使用viewchildren和querylist来获取组件的所有引用吗?然后使用组件的数组索引作为引用?让我试着实现它。你能告诉我问题是什么吗?它可以工作,谢谢。接受了答案,并投了赞成票。