0

My code statement looks like this:

 this.elRef.nativeElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.scrollTo(0, 0);

How could I access an HTML element of an ancestor of the child component(my parent is at the top of the tree)?

I tried with suggested @Host method but seems like I'm getting the copy of Host component, not the parent root element.

Do we have a better syntax for the above statement?

Sandeep Sharma
  • 1,855
  • 3
  • 19
  • 34

1 Answers1

0

in your html put #reference

<html-tag-element #yourParentEl></html-tag-element>

then in your component:

@ViewChild('yourParentEl') parent: any;

and you have in this.parent your element.

Luca Taccagni
  • 1,059
  • 6
  • 23
  • Wow.. getting parent element with @ViewChild and injecting parent component into the child and accessing the element is working as expected. Thank you very much. – Sandeep Sharma Dec 19 '17 at 12:32