简体   繁体   English

Angular 如何使用 ViewChildren 循环 QueryList?

[英]Angular how to loop throught QueryList with ViewChildren?

I have child components in ngFor:我在 ngFor 中有子组件:

@ViewChildren(SingleMultiSelectionComponent) singleMultiSelections: QueryList<SingleMultiSelectionComponent>;

Then i want to loop throught that QueryList:然后我想遍历那个 QueryList:

console.log(this.singleMultiSelections) // 1 item in _results , but results are private
for(let component of this.singleMultiSelections){
  console.log(component); //Not Working
 }

There is nothing wrong with your code per se.您的代码本身没有任何问题。

QueryList implements Iterable, so you can use methods such as forEach , map , and for (let i of items) on it. QueryList 实现了 Iterable,因此您可以在其上使用forEachmapfor (let i of items)等方法。 You can view this in more detail in the Angular Documentation .您可以在Angular 文档中查看更多详细信息。

Keep in mind that for the QueryList to produce the expected results, you can only call it after the LifeCycle hook AfterViewInit has been called.请记住,要让 QueryList 产生预期的结果,您只能在调用 LifeCycle 挂钩AfterViewInit之后调用它。 Until then the list will not have been loaded, you read the official notes .在那之前列表不会被加载,你阅读官方说明

You can do so like this:你可以这样做:

@ViewChildren(SingleMultiSelectionComponent) singleMultiSelections: QueryList<SingleMultiSelectionComponent>;

onAfterViewInit() {
  for(let component of this.singleMultiSelections){
    // Code here
  }
}

Firstly, it depends where you are calling it from.首先,这取决于您从哪里调用它。 Probably you will have to call it in ngAfterViewInit when all the components are instantiated.当所有组件都被实例化时,您可能必须在 ngAfterViewInit 中调用它。 If you are calling before that, those components might not yet exist如果您在此之前调用,这些组件可能还不存在

暂无
暂无

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

相关问题 Angular 如何从 viewChildren queryList 项中获取索引? - Angular how to get index from viewChildren queryList items? 访问 ViewChildren 查询列表的第 n 个子项(角度) - Access nth child of ViewChildren Querylist (Angular) 如何将焦点设置在 ViewChildren QueryList 的第一个元素上? - How to set focus on the first element of a ViewChildren QueryList? 如何使用@viewChildren 在 Angular 中以 QueryList 的形式访问规范文件中的子组件属性 - How to Access child component property in spec file as QueryList in Angular using @viewChildren Angular 动态组件:@ViewChildren 为 QueryList 中的每个组件获取 ViewContainerRef - Angular Dynamic Components: @ViewChildren get ViewContainerRef for every component in QueryList angular 9 使用查询列表(contentChildren 或 ViewChildren)对同级 dom 重新排序/排序 - angular 9 reorder/sort siblings dom by using querylist(contentChildren or ViewChildren) PrimeNg:ViewChildren QueryList始终为空 - PrimeNg: ViewChildren QueryList is always empty 当动态添加二级子级时,Angular2 @ ViewChildren / @ViewQuery的QueryList未更新 - Angular2 @ViewChildren/@ViewQuery's QueryList not updated when dynamically adding second level children 找不到模块:错误:无法解析“@angular/core/src/render3”:ViewChildren QueryList - Module not found: Error: Can't resolve '@angular/core/src/render3': ViewChildren QueryList angular 如何使用查询列表对象? - How querylist object is used by angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM