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 ?

3
  • When you just want to use the first, why not angular.io/api/core/QueryList#first? Commented Oct 12, 2020 at 11:40
  • Maybe this answer can help you ? Commented Oct 12, 2020 at 12:03
  • I think this queryList gives me 3 queries with 1 item , bcs first and last is the same Commented Oct 12, 2020 at 12:08

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.