10

I need to get the child component DOM reference from parent component using angular 4, but i can't access child component DOM, please guide me how to achieve this.

parent.component.html

<child-component></child-component>

parent.component.ts

import { Component, Input, ViewChild, ElementRef, AfterViewInit } from '@angular/core';

@Component({
  selector: 'parent-component',
  templateUrl: './parent.component.html'
})
export class ParentComponent implements AfterViewInit {
  @ViewChild('tableBody') tableBody: ElementRef;
  constructor(){
    console.log(this.tableBody);//undefined
  }
  ngAfterViewInit() {
       console.log(this.tableBody);//undefined
   }
}

child.component.ts

import { Component } from '@angular/core';
@Component({
  selector: 'child-component',
  templateUrl: './child.component.html'
})
export class ChildComponent { 
}

child.component.html

<div>
        <table border="1">
      <th>Name</th>
      <th>Age</th>
      <tbody #tableBody>
        <tr>
         <td>ABCD</td>
          <td>12</td>
        </tr>        
      </tbody>
        </table>
</div> 
Jeyabalan Thavamani
  • 3,057
  • 8
  • 21
  • 33

4 Answers4

14

To expand on Sachila Ranawaka answer:

First you need <child-component #childComp></child-component>

In your parent component, instead of ElementRef it should be ChildComponent:

@Component({
  selector: 'parent-component',
  templateUrl: './parent.component.html'
})
export class ParentComponent implements AfterViewInit {
  @ViewChild('childComp') childComp: ChildComponent;
  constructor(){
  }
  ngAfterViewInit() {
    console.log(this.childComp.tableBody);
  }
}

For your child component:

@Component({
  selector: 'child-component',
  templateUrl: './child.component.html'
})
export class ChildComponent { 
  @ViewChild('tableBody') tableBody: ElementRef;
}
penleychan
  • 5,370
  • 1
  • 17
  • 28
5

Adding to Sachila's and penleychan's good answers, you can reference a component with @ViewChild just by its component's name:

parent.component.html

<!-- You don't need to template reference variable on component -->   
<child-component></child-component> 

Then in parent.component.ts

import { ChildComponent } from './child-component-path';

@Component({
  selector: 'parent-component',
  templateUrl: './parent.component.html'
})
export class ParentComponent implements AfterViewInit {
  @ViewChild(ChildComponent) childComp: ChildComponent;
  constructor(){
  }
  ngAfterViewInit() {
    console.log(this.childComp.tableBody);
  }
}
oneberenjena
  • 145
  • 2
  • 8
1

Need to reference the tableBody from the parent component. So add it to he child-component and remove it from tbody

<child-component #tableBody></child-component>
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
0

just to add here , if you also need to change the DOM element attributes then once you have got access of child component Dom as demonstrated in @penleychan answer. after that just set the color. let parentdiv= this.childComp.tableBody.nativeElement; parentdiv.style.backgroundColor='red';