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>
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.
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.
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.
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;
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With