【发布时间】:2021-04-06 23:39:58
【问题描述】:
我是 Angular 和 Observables 的新手。我的问题是: 在我的组件 HomePage 中,我调用了调用 http.get 并返回 observable 的服务。在我的模板主页上,我对此数据使用异步 $。我需要在 ngOnInit 中调用另一个 void 函数,该函数将在返回时使用 data$。所以我无法弄清楚如何做到这一点。
主页.ts
export class HomePage implements OnInit {
data$: Observable<any>;
ngOnInit(): void {
//I need userId that will be returned here
this.data$ = this.dataService.getMyData();
//how to call this function after data$ is returned?
this.sendMessage(data.userId);
}
sendMessage(data.userId){
//send this userId
}
}
主页.html
<app-homepage form="true" *ngIf="{ data: data$ | async} as myData">
<div *ngIf="data.userId">
///....
我已经尝试过了:但是每次我在页面上输入一些数据时 - 它都会多次调用 sendMessage()...
this.data$ = this.dataService.getMyData().pipe(
tap((data) => {
this.sendMessage(data.userId);
console.log("in pipe")//here I see multiple calls when I click on checkbox for exemple..
}));
【问题讨论】:
标签: angular angular2-observables