簡體   English   中英

查看孩子/查看孩子從父組件到子組件

[英]View child/ View children from parent component to child component

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM