ngFor中动态元素的angular viewChild [英] angular viewChild for dynamic elements inside ngFor

查看:577
本文介绍了ngFor中动态元素的angular viewChild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模板具有以下内容:

My template has something like this:

<div *ngFor="let data of array">
    <div #div></div>
</div>

我是否可以使用#来为每个div分别命名?如果是这样,以后如何在TS文件中访问它们?

is there a way for me to name each div differently using the #? And if so, how can I access them later in my TS file?

顺便问一下,这些#叫什么?我无法搜索它们,因为我现在已经知道它们是什么(我只知道它们的用途)

by the way, what are these # called? I cant search for them since I have now idea what these are (I just know what they're used for)

推荐答案

正如@Commerical Suicide所述,您不能动态设置引用.

As @Commerical Suicide mentioned you cannot set the references dynamically.

#div被称为模板参考变量.

您可以通过 ViewChildren 访问 divs 在您的*.ts文件中.

You can access your divs via ViewChildren in you *.ts file.

示例:@ViewChildren('div') divs: QueryList<ElementRef>

然后,您可以在新的QueryList上调用多个方法,例如forEachfind.

Then you can call multiple methods on your new QueryList, like forEach or find.

这篇关于ngFor中动态元素的angular viewChild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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