use*_*577 0 post call httpclient rxjs angular
我需要使用httpclient进入foreach循环。有可能的?正确的方法是什么?这是我的代码:
this.attachToDelete.forEach(
element => {
this.documentService.DeleteAttachment(element, this.newDocumentId.toString());
console.log("upload delete from submit", element);
}
);
Run Code Online (Sandbox Code Playgroud)
这是对应服务中的方法:
public DeleteAttachment(attachmentId: number, documentId: string) {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json; charset=utf-8');
return this.http.post('http://localhost:8080/DocumentAPI/api/document/RemoveAttachment?docId=' + documentId + '&attachmentId='+attachmentId, attachmentId, { headers: headers }).subscribe(
data => {
if(data)
console.log("delete attach????", data);
else{
console.log('Error occured');
}
}
);
}
Run Code Online (Sandbox Code Playgroud)
小智 6
从您开始以来,我将详细说明:)
恭喜,您似乎成功创建了HTTP调用。
创建HTTP调用时,您将创建一个Observable。可观察的事物就像诺言,但它们做得更多。
可观察对象的问题是,您需要订阅它们才能触发它们。
这可以通过将HTTP调用与链接在一起来完成subscribe:
this.documentService
.DeleteAttachment(element, this.newDocumentId.toString())
.subscribe(response => {...})
Run Code Online (Sandbox Code Playgroud)
response 将包含您的后端退货。
但是我建议您是一次使每个HTTP调用无效。这可以通过在Observable原型上使用一种称为的方法来完成forkJoin。
如果使用forkJoin,则需要创建一个调用数组,如下所示:
const calls = [];
this.attachToDelete.forEach(element => {
calls.push(this.documentService.DeleteAttachment(element, this.newDocumentId.toString()));
});
Observable.forkJoin(calls).subscribe(responses => {...});
Run Code Online (Sandbox Code Playgroud)
这次,响应将是每个呼叫的数组,按您将其放入数组的顺序排序。
我希望这有帮助,如果没有的话,请随时提出任何问题!
(这意味着您还需要subscribe从服务中删除全部内容,因为如果您不这样做,则不会返回可观察到的结果,而是将其返回)
| 归档时间: |
|
| 查看次数: |
2817 次 |
| 最近记录: |