简体   繁体   English

Angular 如何从 viewChildren queryList 项中获取索引?

[英]Angular how to get index from viewChildren queryList items?

I am watching for my radio buttons with :我正在关注我的单选按钮:

  @ViewChildren('radioButton') radioButton: QueryList<MatRadioButton>;

Later I want to get the first and make it checked :后来我想得到第一个并检查它:

  public clickEmitFirst() {
    this.radioButton.forEach((radio,index) => {
        console.log(index);
        radio.checked = true;
        this.clickEvent.emit({ id: this.parentId, item: this.item });
    });
  }

The problem is that index is always 0 (3 console logs with 0)问题是索引始终为 0(3 个控制台日志为 0)

How to fix that ?如何解决?

I've made a demo in which everything works (looping through viewChildren and getting the index).我做了一个演示,其中一切正常(循环遍历 viewChildren 并获取索引)。 Try to compare this, maybe you've made a typo somewhere.试着比较一下,也许你在某个地方打错了。 stackblitz 闪电战

我的错,我在我的子组件中调用了 viewChildren 函数,它只有一个单选按钮(具有单个单选按钮的多个子组件),这就是我在每个组件中获得 0 索引的原因。

暂无
暂无

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

相关问题 Angular 如何使用 ViewChildren 循环 QueryList? - Angular how to loop throught QueryList with ViewChildren? Angular 动态组件:@ViewChildren 为 QueryList 中的每个组件获取 ViewContainerRef - Angular Dynamic Components: @ViewChildren get ViewContainerRef for every component in QueryList 访问 ViewChildren 查询列表的第 n 个子项(角度) - Access nth child of ViewChildren Querylist (Angular) 如何将焦点设置在 ViewChildren QueryList 的第一个元素上? - How to set focus on the first element of a ViewChildren QueryList? 如何从Angular 2中的QueryList获取子元素? - How can I get children elements from QueryList in Angular 2? 如何使用@viewChildren 在 Angular 中以 QueryList 的形式访问规范文件中的子组件属性 - How to Access child component property in spec file as QueryList in Angular using @viewChildren Angular2 +如何从QueryList &lt;&gt;获取正确的元素顺序 - Angular2+ how to get proper element order from QueryList<> 如何在过滤属性时将 ViewChildren QueryList 中的特定项目作为 ElementRef 获取? - How to get specific item in ViewChildren QueryList as ElementRef while filtering on property(ies)? angular 9 使用查询列表(contentChildren 或 ViewChildren)对同级 dom 重新排序/排序 - angular 9 reorder/sort siblings dom by using querylist(contentChildren or ViewChildren) 来自@ViewChildren 的 QueryList 的排序是否总是与 DOM 中的排序相匹配? - Does the ordering of the QueryList from @ViewChildren always match with the ordering in the DOM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM