触发单击嵌套循环Angular ngFor下的特定项目 [英] Trigger click on specific item under nested loop Angular ngFor

查看:88
本文介绍了触发单击嵌套循环Angular ngFor下的特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在嵌套循环下定位指定项目

How to target on the specif item under the nested loop

<ul #parents *ngFor="let parent of parents">
     <li #child *ngFor="let child of parents.childGroup"> {{child.name}} </li>
<ul>

Ts文件

在我的代码中,我的目标是parents[5].child[0],但是它作为子级parents[0].child[0]

Asper my code my target is parents[5].child[0] , but its working as child parents[0].child[0]

@ViewChildren('parents') parents : QueryList<ElementRef>
 @ViewChildren('child') child: QueryList<ElementRef>

this.parents.forEach((item, index) => {
if (index === 5 ) {
(this.child.find( (item , index) => index === 0 ).nativeElement as HTMLElement).click();
}


});

每个父母都有自己的孩子,这里的目标是根据所有孩子的索引计算的

Each parent has own children, Here target calculated based on all child index

如何解决这个问题?

推荐答案

您可以在html元素中设置动态ID ,如下所示:

You can set dynamic id in html element like this:

<ul #parents *ngFor="let parent of parents">
     <li #child *ngFor="let child of parents.childGroup;let i=index" id="{{'child'+ i }}">
          {{child.name}} 
    </li>
<ul>

,然后从打字稿中单击.

and then click from typescript.

this.parents.forEach((item, index) => {
  if (index === 5 ) {
    document.getElementById("child" + index  ).click();
  }
});

这篇关于触发单击嵌套循环Angular ngFor下的特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆