如何在angular中获取fromArray中的elementRef

如何在angular中获取fromArray中的elementRef,angular,Angular,如何从数组访问元素ref。比如#cTab5。 我在ts中使用viewchild访问#cTab5,但不知道如何使用表单数组访问 @ViewChild("cTab5", { static: false }) cTabE5: NbTabComponent; this.campaignLinesAarray().at(Index).get("cTabE5"); <nb-tab tabTitle="Comments" #cTab5>

如何从数组访问元素ref。比如#cTab5。 我在ts中使用viewchild访问#cTab5,但不知道如何使用表单数组访问

  @ViewChild("cTab5", { static: false }) cTabE5: NbTabComponent;
 this.campaignLinesAarray().at(Index).get("cTabE5");

  <nb-tab tabTitle="Comments" #cTab5>                              
   </nb-tab>
@ViewChild(“cTab5”,{static:false})cTabE5:NbTabComponent;
this.campaignLinesAarray().at(Index.get)(“cTabE5”);

实际上,我想要acces引用变量和formArray。我的html如下所示

 <form [formGroup]="add_iroForm">
<div
                  class="accordion-container col-sm-10 col-12"
                  formArrayName="campaign_lines_array"
                  *ngFor="
                    let item of add_iroForm.get('campaign_lines_array')
                      .controls;
                    let i = index
                  "
                >
 <nb-tabset #tabset{{i}} id="acceptable">
  <nb-tab tabTitle="Comments" #cTab1>                              
   </nb-tab>
    <nb-tab tabTitle="Comments" #cTab2>                              
   </nb-tab>
    <nb-tab tabTitle="Comments" #cTab3>                              
   </nb-tab>
</nb-tabset>
</div>
</form>

如果在*ngFor中有一个参考变量,则需要使用-in code-,如果需要在.html中使用该变量,请考虑这一点,例如

<div #item *ngFor="let i of [0,1,2,3]" >
   <button (click)="click(item)">button</button>
</div>
<button (click)="showmeAll()">button</button>

@ViewChildren('item')items:QueryList<ElementRef>
click(item)
{
   console.log(item.innerHTML)
}
showmeAll()
{
  this.items.forEach(x=>{
    console.log(x.nativeElement.innerHTML)
  })
}
Update2我们可以使用一个函数来激活选项卡集的选项卡

   setActiveTab(indexTabSet,indexTab)
   {
        this.tabs.filter(x=>x.tabId.endsWith("-"+indexTabSet)).forEach(x=>{
          x.active=x.tabId.startsWith("tab"+indexTab+"-")
        })
   }

你的问题不清楚。清楚地解释你想做什么。实际上我想通过formArray访问引用变量,因为引用变量已经在formArray中了。this.campaignLinesAarray().at(empinex.get)([“tabset”,0])@也许突然间,tabs对你的正式约会一无所知。formArray只是一个formArray,不知道yu是否在使用输入、选项卡集或任何东西。阅读有关Nebular的内容,我更新了答案,以显示如何使用ViewChildren管理“选项卡”,我希望这对您有所帮助。显示错误-无法读取未定义的属性“startsWith”,还想知道如何动态设置选项卡集。您是否为所有“选项卡”赋予了[tablId]值?我获得了单独的选项卡,但希望动态设置和获取选项卡集,因为有多个选项卡集,因为用于selectTab,例如this.tabsetl.selectTab(this.tab1);this.tabsetl.selectTab(this.tab2);this.tabset2.selectTab(this.tab1);this.tabset2.selectTab(this.tab2);
@ViewChildren('tabset',{read:NbTabsetComponent)items:QueryList<NbTabsetComponent>
@ViewChildren(NbTabsetComponent)items:QueryList<NbTabsetComponent>
  @ViewChildren(NbTabComponent) tabs:QueryList<NbTabComponent>
  click()
  {
    this.tabs.forEach(x=>{
      x.active=x.tabId.startsWith("tab1")
    })
  }
  click2(index)
  {
    this.tabs.filter(x=>x.tabId.endsWith("-"+index)).forEach(x=>{
      x.active=x.tabId.startsWith("tab0")
    })
  }
   setActiveTab(indexTabSet,indexTab)
   {
        this.tabs.filter(x=>x.tabId.endsWith("-"+indexTabSet)).forEach(x=>{
          x.active=x.tabId.startsWith("tab"+indexTab+"-")
        })
   }