如何使用 angular 4 从父元素获取子元素 DOM 引用
How to get the child DOM element reference from Parent using angular 4
我需要使用 angular 4 从父组件获取子组件 DOM 引用,但我无法访问子组件 DOM,请指导我如何实现此目的.
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>
需要从父组件引用 tableBody
。所以将它添加到 child-component
并将其从 tbody
中删除
<child-component #tableBody></child-component>
要扩展 Sachila Ranawaka 的回答:
首先你需要<child-component #childComp></child-component>
在你的父组件中,而不是 ElementRef
它应该是 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);
}
}
对于您的子组件:
@Component({
selector: 'child-component',
templateUrl: './child.component.html'
})
export class ChildComponent {
@ViewChild('tableBody') tableBody: ElementRef;
}
除了 Sachila 和 penleychan 的好答案之外,您还可以仅通过其组件名称来引用具有 @ViewChild
的组件:
parent.component.html
<!-- You don't need to template reference variable on component -->
<child-component></child-component>
然后在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);
}
}
我需要使用 angular 4 从父组件获取子组件 DOM 引用,但我无法访问子组件 DOM,请指导我如何实现此目的.
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>
需要从父组件引用 tableBody
。所以将它添加到 child-component
并将其从 tbody
<child-component #tableBody></child-component>
要扩展 Sachila Ranawaka 的回答:
首先你需要<child-component #childComp></child-component>
在你的父组件中,而不是 ElementRef
它应该是 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);
}
}
对于您的子组件:
@Component({
selector: 'child-component',
templateUrl: './child.component.html'
})
export class ChildComponent {
@ViewChild('tableBody') tableBody: ElementRef;
}
除了 Sachila 和 penleychan 的好答案之外,您还可以仅通过其组件名称来引用具有 @ViewChild
的组件:
parent.component.html
<!-- You don't need to template reference variable on component -->
<child-component></child-component>
然后在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);
}
}