来自另一个组件的 Angular 9 调用函数

标签 angular

我有一个组件让我已经在使用它的 app-main 另一个组件及其事件发射器:

<app-dashboard (dateFrom)=OnDateFrom($event) ></app-dashboard>

<app-dashboard>我有一个这样的函数:

@Output() weatherResult= new EventEmitter<any>();

  SendWeatherResult(){

this.weatherResult.emit(results);
}

现在我需要调用 SendWeatherResult在我的组件中运行

<app-dashboard (dateFrom)=OnDateFrom($event) //here i need to call this function></app-dashboard>

我想在我的主应用程序中调用 SendWeatherResult 函数

最佳答案

解释可能更清楚,但也许您需要的是 template variables :

<app-dashboard #appDashboard (dateFrom)=OnDateFrom($event)></app-dashboard>
<button (click)="appDashboard.SendWeatherResult()"> Send weather result</button>

如果你想从组件 .ts 中使用它,请使用 viewChild :

@ViewChild('appDashboard', { static: false }) appDashboard: DashboardComponent;

callSendWeatherResult(): void {
this.appDashboard.SendWeatherResult()
}

关于来自另一个组件的 Angular 9 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65320884/

相关文章:

angular - ngModel 未正确绑定(bind)

angular - Azure map 控制导致 Angular 通用 SSR 错误

子模块的 Angular 合并路线

javascript - 使用 Angular 4 Form Group 编辑和添加新数据

javascript - 切换子组件时如何隐藏父组件

javascript - 使用 Writefile Api (Ionic-Cordova) 创建 JPEG 文件后无法打开

css - Accordion 组标题的样式(ngx-bootstrap)?

Angular 2 : Swapping between different components without destroying them

javascript - 显示 Angular 2 JSON 对象

Angular 2,使用 Angular Cli 和 Ui Router Ng2,解决/拒绝