Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the child DOM element reference from Parent using angular 4

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> 
like image 437
Jeyabalan Thavamani Avatar asked Dec 15 '17 05:12

Jeyabalan Thavamani


People also ask

How do you access the child component property in the parent component?

We can get child component values in the parent component by creating a reference to the child component using the @ref directive in the Parent component. Using the reference instance, you can access the child component values in the parent.

How do you style a child component from the parent component?

use @Component to add css class to host element and set encapsulation to none. Then reference that class which was added to the host within the components style. css. scss This will allow us to declare styles which will only affect ourselves and our children within scope of our class.

How will you share the data from parent to child component in Angular?

To let Angular know that a property in a child component or directive can receive its value from its parent component we must use the @Input() decorator in the said child. The @Input() decorator allows data to be input into the child component from a parent component.


2 Answers

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;
}
like image 80
penleychan Avatar answered Oct 24 '22 21:10

penleychan


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);
  }
}
like image 26
oneberenjena Avatar answered Oct 24 '22 20:10

oneberenjena