[英]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.