Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way in angular 6 to get ViewChildren deep?

Tags:

angular

is there any way to get the children from a subcomponent?

Imagine the following component tree:

  • App
    • Question
      • Question Option (contains a checkbox)
      • Question Option (contains a checkbox)
      • Question Option (contains a checkbox)

I want to access Question Option from App to check all checkboxes.

See https://stackblitz.com/edit/angular-ghvsuu for an example.

like image 625
snd Avatar asked Jun 06 '18 13:06

snd


1 Answers

You can do it in different way though.

Add @ViewChildren(QuestionOptionComponent) options in QuestionComponent instead of AppComponent

And in AppComponent

export class AppComponent  {

  @ViewChild(QuestionComponent) question

  constructor(private elementRef: ElementRef) {}

  checkAll() {
    console.log("this.question", this.question) // found
    console.log("this.options", this.question.options) // found

  }
}

Forked DEMO

like image 169
Amit Chigadani Avatar answered Oct 21 '22 23:10

Amit Chigadani