12

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;
    } 
Sujatha Girijala
  • 1,141
  • 8
  • 20
Sebastian Hernandez
  • 2,257
  • 4
  • 23
  • 32

5 Answers5

30
constructor(elementRef:ElementRef) {
  elementRef.nativeElement.parentElement....
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    Could you explain this a bit? Preferably with an example ? Why are there trailing dots in the last ? Which object/variable is the child able to use now ? Is this to be added in child or parent component? – Rohit Kumar Oct 12 '20 at 13:22
  • 1
    The parent dots are your custom code This code is the child element. `elementRef` points to "self" and "elementRef.nativeElement.parentElement" points to the parent element. If you want to access the component instead of the element then you need to inject the component. https://angular.io/guide/dependency-injection-navtree#find-a-parent-component-of-known-type – Günter Zöchbauer Oct 12 '20 at 16:32
4

In your child component pass parent in constructor:

  constructor(
    private parent: ParentComponent,
    ...
  )

  ngAfterViewInit() {
    console.log(this.parent);
  }
Tukkan
  • 1,574
  • 2
  • 18
  • 33
2

The child component shouldn't update properties on the parent. Instead have the child emit events and the parent change the properties. The docs have an example in the Component Interaction section.

JayChase
  • 11,174
  • 2
  • 43
  • 52
0

for accessing values of parent you can use @input. for changing parent values try this example

child component

import { Component, EventEmitter, Input, Output } from '@angular/core';

@Component({
  selector: 'rio-counter',
  templateUrl: 'app/counter.component.html'
})
export class CounterComponent {
  @Input()  count = 0;
  @Output() result = new EventEmitter<number>();

  increment() {
    this.count++;
    this.result.emit(this.count);
  }
}

child html part

<div>
  <p>Count: {{ count }}</p>
  <button (click)="increment()">Increment</button>
</div>

parent component

import { Component } from '@angular/core';

@Component({
  selector: 'rio-app',
  templateUrl: 'app/app.component.html'
})
export class AppComponent {
  num = 0;
  parentCount = 0;

  assignparentcount(val: number) {
    this.parentCount = val;
  }
}

parent html part

<div>
  Parent Num: {{ num }}<br>
  Parent Count: {{ parentCount }}
  <rio-counter [count]="num" (result)="assignparentcount($event)">
  </rio-counter>
</div>
Amit kumar
  • 6,029
  • 6
  • 30
  • 40
0

If you want to get data from the parent component, you may want to review data exchange methods between components. It seems to me that you may want to take a look at the Input decorator. You can do something like: <app-spinner show="true"></app-spinner> Or <app-spinner show="isBusy()"></app-spinner>

Take a look at it here: https://angular.io/guide/inputs-outputs

  • I think that what the question is asking is the exact opposite, something that uses ```@Output``` and listens for it in the parent. – Gianmarco Nov 04 '20 at 09:43