: 구성 요소 (옵션 2)의 내부
import { ..., ViewChild, ... } from '@angular/core';
// ...
export class ParentComponent {
@ViewChild('yourChild') child;
ngAfterViewInit() {
console.log('only after THIS EVENT "child" is usable!!');
this.child.hide();
}
}
:
import { ..., ViewChild, ... } from '@angular/core';
import { ..., ChildComponent, ... } from '....';
// ...
export class ParentComponent {
@ViewChild(ChildComponent) child;
ngAfterViewInit() {
console.log('only after THIS EVENT "child" is usable!!');
this.child.hide();
}
}
공식 문서를 참조하십시오 구성 요소 (옵션 1)의 내부
<parent (click)="yourChild.hide()">
<child #yourChild></child>
</parent>
: https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-view-child
라이브 데커 : https://plnkr.co/edit/fEYwlKlkMbPdfx9IGXGR?p=preview
우수. 따라서 @ViewChild에 클래스 이름을 전달할 때 템플릿에 의해 인스턴스화 된 ChildComponent를 자동으로 선택합니다. – BBaysinger
정확하게, 나는 그것의 첫 번째 또는 마지막 경우 여러 자식 있는지 확실하지 않습니다. 하지만 여러 자식들에게'ViewChildren'을 사용할 수 있습니다! 같은 방식으로 이름이나 클래스를 사용합니다. – mxii
데모 플 런커를 추가했습니다. 동일한 클래스의 자식이 여러 개 있으면 'ViewChild'가 처음 발견 된 자식을 선택합니다. ;) – mxii