[英]How to merge two observables in Angular/RxJS?
In my service I have these methods:在我的服务中,我有这些方法:
getMap(): any {
return this.http.get(`someURL`);
}
getAssets(): any {
return this.http.get(`someURL`);
}
In my Component I use them like this:在我的组件中,我像这样使用它们:
ngOnInit() {
this.myService.getMap().subscribe(data => {
this.map = data; // Returns ["map-1.svg", "map-0.svg"]
});
this.systemMapService.getAssets().subscribe(data => {
this.assets = data; // Returns ["map-mapping-0.json", "map-mapping-1.json"]
});
}
In my template I want to use it like this:在我的模板中,我想像这样使用它:
<mat-tab-group mat-align-tabs="end">
<div *ngFor="let item of assets; let i = index">
<mat-tab label="{{i}}">
<div class="container">
<div class="map">
<img id="img_equipment" [src]="apiUrl + '/path/to/svg/' + item">
<a *ngFor="let link of map"
title="{{ link.title }}"
class="ink"
[ngStyle]="{ left: link.left, top: link.top, width: link.width }">
</a>
</div>
</div>
</mat-tab>
</div>
</mat-tab-group>
I provided the return values of the calls as comments in die code.我提供了调用的返回值作为代码中的注释。
For example, the file map-0.svg
should use this JSON as a mapping map-mapping-0.json
.例如,文件
map-0.svg
应使用此 JSON 作为映射map-mapping-0.json
。 The file map-1.svg
then in turn this JSON file map-mapping-1.json
.文件
map-1.svg
然后又是这个 JSON 文件map-mapping-1.json
。
..and do the arrays that the calls return have to be sorted for it to work? ..并且调用返回的数组是否必须排序才能工作? Because unfortunately they are currently being returned unsorted by the backend.
因为不幸的是,它们目前被后端未排序地返回。
From your template and description, it's not entirely clear what shape your data should take to make this easiest for you.从您的模板和描述来看,您的数据应该采用什么形状才能使您最轻松地完成这项工作并不完全清楚。 Regardless, my instinct here would be to transform your data until it's easily consumed by your template and then use angular's async pipe.
无论如何,我的直觉是转换您的数据,直到您的模板轻松使用它,然后使用 angular 的异步管道。
// I'm making this an array of strings tuples, but it can
// be whatever best serves your purpose
displayData = Observable<Array<[string, string]>>;
ngOnInit() {
this.displayData = forkJoin({
mapData: this.myService.getMap(),
mapAssets: this.systemMapService.getAssets()
}).pipe(
map(({mapData, mapAssets}) => {
const formattedData = mapData.map(mapInstance => {
// You're going to have to define this function yourself
// to effective organsize/sort/whatever your data.
const assetInstance = this.extractMapAsset(mapAssets, mapInstance);
return [mapInstance, assetInstance];
});
return formattedData;
})
);
}
// In template
<div *ngFor='let tupleData of displayData | async'>
The map is called {{tupleData[0]}}
and its assetJson is called {{tupleData[1]}}
</div>
This is a grossly simplified example, but it gets the basic structure.这是一个非常简化的示例,但它获得了基本结构。 You join and format your data and then you display it in your view.
您加入并格式化您的数据,然后将其显示在您的视图中。 In this example, it's just a tuple of strings, but it can be anything such that you view can have nested
ngFor*
calls displaying your data however you like.在这个例子中,它只是一个字符串元组,但它可以是任何你认为可以嵌套
ngFor*
调用来显示你喜欢的数据的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.