我想創建一個嵌套的可觀察性來保存與標記相關的一個內容。首先保存內容並返回內容標識,以便將內容標識作爲外鍵保存,方法與在我的循環中同時發送數據的方式類似。 (後端日誌運行pararel)我怎樣才能使它等到做下一步要做一個可觀察的嵌套循環
component.ts
save(){
this.contentService.savecontent(this.content_input) // save content
.subscribe(
data => { this.listContentData = data }, // return data to get content id
error => { error = error },
() => {
this.savetag(this.listContentData); // save tag
});
}
savetag(listcontentdata): void {
// listdraftCategories is a list of tag
for (var i = 0; i < this.listdraftCategories.length; i++) {
this.tagService.savetagwithcontent(this.listdraftCategories[i], listcontentdata)
.subscribe(
data => { data = data },
error => { },
() => {
});
}
}
service.ts
savecontent(contentObj: any): Observable<any> {
contentObj = JSON.parse(contentObj);
let body = JSON.stringify(
{
"token": "test",
"content": {
"contentName": contentObj.itemName // contentId will be autoincrement in backend
}
}
}
);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(this.host + this.url_content + this.url_save, body, options)
.map(res => this.extractData(res))
.catch(this.handleError);
}
savetagwithcontent(tagObj: any, contentObj: any): Observable<any> {
console.log("obj", contentObj);
let body = JSON.stringify(
{
"token": "test",
"content": {
"fkTag": {
"tagId": tagObj.tagId
},
"fkContent": {
"contentId": contentObj.responseObject[0].contentId
}
}
}
);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(this.host + this.url_tag_has_content + this.url_save, body, options)
.map(res => this.extractData(res))
.catch(this.handleError);
}
後臺日誌(保存的內容標籤)
--- start save tag with content ---
--- start save tag with content ---
--- content id : 1 tag id: 1 ---
--- content id : 1 tag id: 2 ---
--- end save tag with content ---
--- end save tag with content ---
看起來像它在幾乎同時在方法中。我如何設法使savetag等待一個保存完成然後做下一個?
你的代碼它的工作,但我需要通過添加if(index
哦,是的抱歉我做了這個例子,告訴你如何添加一個遞歸方法,讓我解決這個錯誤。 – Supamiu
如果我有其他標籤列表,我該如何使用相同的方法來管理它? (如listdraftIndustries,listdraftCompany如何保存savetag方法,如果它不是相同的列表,我不能使用相同的索引權?) –