如何发出POST请求以按角度保存层次结构中的对象列表 [英] How to make POST requests to save a list of objects in the hierarchical structure in angular
问题描述
我有一个层次结构中的对象列表,这些对象与parentId相关,例如:
I have a list of objects in the hierarchical structure which are related by parentId, for example:
[
{
id: 10,
name: "Parent Unit 1",
parentId: null,
children:
[
{
id: 11,
name: "Unit Child 1.1",
parentId: 10,
children:
[
{
id: 15,
name: "Unit Child 1.1.1",
parentId: 11,
children: []
]
},
{
id: 12,
name: "Unit Child 1.2",
parentId: 10,
children: []
}
]
},
{
id: 13,
name: "Parent Unit 2",
parentId: null,
children:
[
{
id: 14,
name: "Unit Child 2.2",
parentId: 13,
children: []
}
]
]
我需要通过调用POST方法六次来通过Web服务一一保存对象.要求是我必须顺序保存它们.我的意思是,当Web服务返回父对象的新ID时,我必须等到保存父单元,然后我才能向Web服务发出另一个POST请求,并为Unit Child
对象修改parentID.
I need to save objects one by one through web service by calling POST method six times. The requirement is that I have to save them sequentially. I mean, I have to wait until Parent Unit is saved, when the web service returns new id of the parent object and then I can make another POST request to web service with modified parentId for Unit Child
objects.
我使用Observables在我的角度应用程序中制作Web服务.有什么想法我该怎么做?
I use Observables for making web services in my angular app. Any ideas how can I do it?
推荐答案
您可以使用switchMap
这是其工作原理的基本示例
Here's a basic example of how it works
Rx.Observable.of('some_url')
.switchMap(url => this.http.get(url))
基本上,您会获得第一个ID,将其切换映射以在第二个调用中使用它,等等...
Basically, you get the first id, switchmap it to use it on the 2nd call etc...
在此处了解更多信息: https://www.learnrxjs.io/operators /transformation/switchmap.html
Read more about it here: https://www.learnrxjs.io/operators/transformation/switchmap.html
这篇关于如何发出POST请求以按角度保存层次结构中的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!