我是 angular 新手,我想為按鈕添加加載狀態,但我不想為其提供任何服務。我真的被管道弄糊涂了,我不明白正確的語法。當 this.apiService.exportReport 完成時,我可以將 isLoading 設定為 false 嗎?
public exportReportResults(format: ExportFormat) {
this.isLoading = true;
this.sink.add(
this.service.context.pipe(
filter((value) => !!value))
.subscribe(
(data) => {
const items: any[] = [];
data.context.groups.forEach(function (group) {
items.push(group.items.map(({ id }) => id));
});
const itemQuery: string = "'{" items.toString() "}'";
this.apiService.exportReport(format, itemQuery);
this.isLoading = false; //not good this way of course
},
)
);
}
uj5u.com熱心網友回復:
Subscribe 接受 3 個引數:值消費者、例外處理程式、終結器。
您可以在 observable 關閉后使用終結器來設定所需的狀態。
如果您的 observable 根本沒有關閉,您必須在消費者中(就像您所做的那樣)或在管道中的某個地方使用 tap 運算子
例如
this.service.context.pipe(
filter((value) => !!value)),
tap(v=>this.isLoading=false)).subscribe(...);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/335913.html
上一篇:Docker Angular:docker引數不適用于ngbuild
下一篇:將物件的變化通知給父角度
