简体   繁体   English

使用 QueryList 给出第一个 HTML 元素

[英]Using QueryList gives the first HTML element

I'm using Angular 10, and trying to use QueryList, with the code below (just like many examples on the web)我正在使用 Angular 10,并尝试使用 QueryList,代码如下(就像网络上的许多示例一样)

HTML HTML

<div *ngFor="let i of [1,2,3,4]" #someID>some content</div>

TS TS

  @ViewChild('someID') someIds: QueryList<ElementRef>;

When running console.log(this.someIds) in the component, after the component initialization, I'm actually getting an ElementRef object (probably the first or the last div) and not the whole list.在组件中运行 console.log(this.someIds) 时,在组件初始化之后,我实际上得到了一个 ElementRef 对象(可能是第一个或最后一个 div)而不是整个列表。

在此处输入图片说明

What am I doing wrong?我究竟做错了什么?

更改ViewChildViewChildren

@ViewChildren('someID') someIds: QueryList<ElementRef>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM