angular - View child/ 从父组件查看子组件到子组件
问题描述
我想 ViewChild 从父组件到子组件。
class ParentComponent {
@ViewChild('CommentSection') commentBox: ElementRef
}
class ChildComponent {}
child.component.html
<textarea #CommentSection>
</textarea>
可能吗?
我在 afterviewInit 函数中得到“未定义”
解决方案
您可以像这样访问子组件
import { Component, ViewChildren, AfterViewInit, QueryList } from '@angular/core';
import { MyComponent } from './mycomponent.component';
export class ParentComponent implements AfterViewInit
{
@ViewChildren(MyComponent) childrenComponent: QueryList<MyComponent>;
public ngAfterViewInit(): void
{
this.childrenComponent.changes.subscribe((comps: QueryList<MyComponent>) =>
{
// Now you can access the child component
});
}
}
推荐阅读
- powershell - SCCM Powershell 非常详细的部署状态消息
- odoo - Odoo 10 中有哪些可用的字段类型?
- android-studio - 主要活动未在 AdroidManifest.xml 中声明
- c++ - 如何将文件的内容读入 C++ 中的 char16_t 数组?
- tensorflow - 为什么Ubuntu16.04 cuda8.0系统不能安装tensorflow-gpu
- css - 从 Chrome 68 到 69 的 CSS 转换行为的奇怪差异
- python - 将 URL 分配给 python 脚本以在 Google Cloud 上运行 cron 作业?
- c++ - 如何在 C++ 中以非常初级的水平显示四分之一圆?
- android-studio - Genymotion android动画故障?
- python - 使用 Python dict 以编程方式创建带有 nbformat 的 Jupyter notebook