ng组件通讯的几种方式

时间:2023-03-09 07:31:28
ng组件通讯的几种方式

通过输入型绑定把数据从父组件传到子组件。

如<app-hero-child *ngFor="let hero of heroes"  [hero]="hero"  [master]="master">

父组件监听子组件的事件。

如<app-voter *ngFor="let voter of voters" [name]="voter" (onVoted)="onVoted($event)">

父组件调用@ViewChild()。

如 @ViewChild(countdownTimer):countdownTimer 这样就可以在父组件中调用子组件的方法了。

其中父组件模板中<app-countdown-timer #countdownTimer></app-countdown-timer>

父组件和子组件通过服务来通讯。