【问题标题】:View child/ View children from parent component to child componentView child/ 从父组件查看子组件到子组件
【发布时间】:2019-03-15 03:52:51
【问题描述】:

我想从父组件到子组件查看Child。

class ParentComponent {
  @ViewChild('CommentSection') commentBox: ElementRef
}

class ChildComponent {}

child.component.html

<textarea #CommentSection>
 </textarea>

有可能吗?

我在 afterviewInit 函数中得到“未定义”

【问题讨论】:

标签: angular angular5 angular6


【解决方案1】:

你可以像这样访问子组件

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
        });
    }
}

【讨论】:

  • 谢谢,Ketan 我可以通过它访问子组件中的元素并隐藏/显示它吗?
猜你喜欢
  • 2020-08-21
  • 2018-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-20
  • 2018-03-08
  • 1970-01-01
相关资源
最近更新 更多