Ich muss die DOM-Referenz der untergeordneten Komponente von eckigen 4, aber ich kann nicht auf Child-Komponente DOM zugreifen, bitte mich, wie dies zu erreichen.So erhalten Sie die untergeordnete DOM-Element Referenz von Parent mit eckigen 4
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>
danke, es funktioniert gut. – thilagabala