Angular 查看子组件/查看从父组件到子组件的子组件
我想从父组件到子组件查看子组件Angular 查看子组件/查看从父组件到子组件的子组件,angular,angular5,angular6,Angular,Angular5,Angular6,我想从父组件到子组件查看子组件 class ParentComponent { @ViewChild('CommentSection') commentBox: ElementRef } class ChildComponent {} child.component.html <textarea #CommentSection> </textarea> 可能吗 我在afterviewInit函数中得到“未定义”您可以像这样访问子组件 import { Co
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
});
}
}
从'@angular/core'导入{Component,ViewChildren,AfterViewInit,QueryList};
从“./MyComponent.component”导入{MyComponent};
导出类ParentComponent实现AfterViewInit
{
@ViewChildren(MyComponent)childrenComponent:QueryList;
public ngAfterViewInit():void
{
this.childrenComponent.changes.subscribe((comps:QueryList)=>
{
//现在您可以访问子组件了
});
}
}
您可以像这样访问子组件
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
});
}
}
从'@angular/core'导入{Component,ViewChildren,AfterViewInit,QueryList};
从“./MyComponent.component”导入{MyComponent};
导出类ParentComponent实现AfterViewInit
{
@ViewChildren(MyComponent)childrenComponent:QueryList;
public ngAfterViewInit():void
{
this.childrenComponent.changes.subscribe((comps:QueryList)=>
{
//现在您可以访问子组件了
});
}
}
请在堆栈上创建一个Blitz。请在堆栈上创建一个Blitz。谢谢,Ketan我可以通过它访问子组件中的元素并隐藏/显示它吗?谢谢,Ketan我可以通过它访问子组件中的元素并隐藏/显示它吗?