angular - 在 Angular 中重新渲染视图模板的差异 Observable 等待和 setTimeout
问题描述
我有排序数据的本地存储,这可能需要一段时间:
private store: IBacktest[] = [];
public setStore(content: string): Observable<void> {
return new Observable<void>((observer) => {
this.store = content
.trim()
.split('\n')
.map(item => toTestCase(item))
.sort((a, b) => b.profitToDropDown - a.profitToDropDown);
observer.next();
observer.complete();
}
);
}
元素的数量 5000 - 10000 并且它得到 10 - 30 秒,因为toTestCase
,这不是一个便宜的操作。我想渲染加载器一个更用户友好的界面:
this.isLoader = true;
this.backtestService
.setStore(content)
.pipe(finalize(() => {
this.isLoader = false;
}))
.subscribe(() => {...});
并<app-loading-overlay *ngIf="isLoader"></app-loading-overlay>
在模板中。
但是什么时候isLoader = true
我可以在调试模式下看到它加载覆盖而不是渲染。
但是当我尝试setTimeout()
像这样测试它的逻辑时:setTimeout(() => { this.isLoader = false; }, 100000)
一切正常并且加载器是可见的。
我的可观察使用有什么问题,以及如何在没有多余的包装纸setTimeout()
或类似拐杖的情况下解决它?
解决方案
我认为这里的问题isLoader
是没有更新他在 DOM 中的值。
让我们尝试使用get/set
我们知道他们会更新他们的价值。
private _isLoader: boolean = false;
public get isLoader() {
return this._isLoader;
}
public set isLoader(value: boolean) {
this._isLoader = value;
}
callsetStore(): {
this.isLoader = true;
this.backtestService
.setStore(content)
.pipe(finalize(() => {
this.isLoader = false;
}))
.subscribe(() => {...});
}
然后在html中:
<app-loading-overlay *ngIf="isLoader"></app-loading-overlay>
有了这个,我认为它应该知道。尝试一下,如果它不起作用,我们将尝试另一种方法。
推荐阅读
- html - 点击后如何隐藏选择选项?
- firebase - 当我尝试打印 Flutter Cards 的 Text 值(实时获取值)并且它总是打印最终卡值时。如何在 Flutter 中解决这个问题
- spring-security-oauth2 - 无法将身份验证和资源服务器组合到单个项目中
- cryptography - 将资产链接到 solana 代币
- azure-active-directory - 如何在兑换邀请后将 Microsoft 帐户配置文件数据复制到 AzureAD 外部访客帐户
- node.js - 每天重置计数器猫鼬序列
- java - Infinispan:ISPN004016:服务器不在集群中(127.0.0.1:11222),从池中删除
- python - 矩阵“a”未重置为空矩阵
- python - 字符串上的多次拆分
- c# - 共享课程与招摇生成的课程