3

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)

How to fix that ?

Devla
  • 326
  • 4
  • 19

2 Answers2

1

I've made a demo in which everything works (looping through viewChildren and getting the index). Try to compare this, maybe you've made a typo somewhere. stackblitz

0

My foult, I was calling viewChildren function in my child component which has only one radioButton (Multiple child components with single radioButtons), thats why I was getting 0 index in each component.

Devla
  • 326
  • 4
  • 19