How to get the parent DOM element reference of an angular2 component

Sebastian Hernandez picture Sebastian Hernandez · Dec 22, 2016 · Viewed 19.8k times · Source

I need to access to some properties of the parent DOM element which contains the component from where i want to get the info, is there a way to do such thing?

Here is how my component looks like:

import { Component, Input } from "@angular/core";

import "./loadingSpinner.component.css!";

    @Component({
        selector: "loading-spinner",
        template: `
            <div *ngIf="showSpinner" class="loader-directive-wrapper">
                <div class="loader"></div>
            </div>`
    })
    export class LoadingSpinnerComponent {
        @Input() public showSpinner: boolean = false;
    } 

Answer

G&#252;nter Z&#246;chbauer picture Günter Zöchbauer · Dec 22, 2016
constructor(elementRef:ElementRef) {
  elementRef.nativeElement.parentElement....
}