如何在 angular 管道中异步设置变量?
How to set a asynchronously a variable in an angular pipe?
我想为按钮添加加载状态,但我不想为其提供任何服务。
我真的被管道弄糊涂了,我不明白正确的语法。当 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
},
)
);
}
订阅有 3 个参数:值消费者、异常处理程序、终结器。
您可以在 observable 关闭后使用终结器设置所需的状态。
如果您的 observable 根本没有关闭,您必须在消费者中执行此操作(就像您所做的那样)或在管道中的某处使用 tap 运算符
例如
this.service.context.pipe(
filter((value) => !!value)),
tap(v=>this.isLoading=false)).subscribe(...);
我想为按钮添加加载状态,但我不想为其提供任何服务。 我真的被管道弄糊涂了,我不明白正确的语法。当 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
},
)
);
}
订阅有 3 个参数:值消费者、异常处理程序、终结器。
您可以在 observable 关闭后使用终结器设置所需的状态。
如果您的 observable 根本没有关闭,您必须在消费者中执行此操作(就像您所做的那样)或在管道中的某处使用 tap 运算符
例如
this.service.context.pipe(
filter((value) => !!value)),
tap(v=>this.isLoading=false)).subscribe(...);