angular - 从 subscribe() Angular 7 中获取价值
问题描述
我已经测试了不同的解决方案,例如添加地图或 switchMap 但我迷路了,我仍然无法从服务功能的订阅中获取数据。
我有服务功能:
getAllExperiments (): Observable<Experiment[]> {
return this.http.get<Experiment[]>(this.urlEndPoint)
.pipe(
catchError(this.handleError<Experiment[]>('getExperiments', []))
);
}
和中的调用ngOnInit
:
async ngOnInit() {
await this.experimentService.getAllExperiments()
.subscribe( response => {
this.expTable = response
this.expId = this.expTable.length;
console.log(this.expTable, this.expId) //appears after the other console.log but with the values
})
console.log(this.expId) //appears first and undefined
await this.experimentService.getExperiment(this.expId)
.subscribe(response => {
this.experiment = response
});
await this.compareService.getScore(this.expId)
.then(response => {
this.expScore = response;
})
}
}
显然getExperiment
带有 ID 的功能也不能正常工作。
我知道它应该能够与 a 一起使用,toPromise
但我不知道如何在这里使用它。
解决方案
推荐阅读
- python - 静态和jinja2 html之间的css结果差异
- regex - 匹配“0xffffffc0006e0584 在 some_function (/path/to/my/file.c:93) 中”的 Bash 正则表达式。
- python - 用 PIL 右对齐文本?
- node.js - 使用 for 循环将 json 插入 postgresql 不会插入我的所有数据?
- pointers - MQL4/5 CList 搜索方法总是返回空指针
- python - 如何从元组列表中排除某些“列”?
- javascript - 如何将全选选项添加到 jquery 过滤器
- python - 检查 3 个字符串输入是否按字母顺序排列
- elasticsearch - 当 java 对象存储到弹性搜索时,日期字段会转换为不同的格式?
- mysql - 查询查找引用表的总和结果