Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 查看子组件/查看从父组件到子组件的子组件_Angular_Angular5_Angular6 - Fatal编程技术网

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我可以通过它访问子组件中的元素并隐藏/显示它吗?