javascript - 进度条不更新角度
问题描述
假设我有一个组件 html,其中有我的 html 我的进度条
<round-progress #progress
[current]="current"
</round-progress>
电流是百分比值。在我的 ts 组件中,我这样做:
this.percentageSubscription=this.percentageService.percentage$.subscribe((percentage)=>{
this.current=this.current+percentage;
console.log(this.current);
});
我打印的值是正确的。它是按照另一种算法增加的。console.log 显示正确的增量,但我的百分比值在进度条中没有增加。任何人都可以帮助我吗?
解决方案
由于您的值确实在您的订阅中正确更新,但在您的模板中没有正确更新,因此更改检测无法注册新值。您可以手动触发更改检测,但您不应该这样做。
我建议使用async
管道。从而将您的用法重写为:
<round-progress #progress
[current]="current$ | async"
</round-progress>
和:
this.current$ = this.percentageService.percentage$.pipe(
scan((acc = 0, percentage) => acc + percentage)
);
作为奖励,您不必再管理您的订阅。如果您在本地需要“当前”用于其他目的,您可以共享 observable(并且必须再次管理订阅),或者您可以添加一个设置本地值的水龙头。
推荐阅读
- vue.js - 当我尝试通过道具从子组件更新主组件数据时,我收到此错误
- php - 未捕获的 PDOException: SQLSTATE[HY093]:Invalid parameter number: 绑定变量的数量与标记的数量不匹配
- spring - 使用键作为字段名称的索引 HashMap。休眠搜索
- r - 从前两行凌乱的 Excel 数据中的字符在 R 中创建描述性变量名称
- apache - Apache ProxyPass 优化与通过重写规则进行代理
- flutter - Flutter WITH Django Rest Api 获取数据
- github - 过滤掉我已经批准的 GitHub PR
- java - 连接到特定网络 - Android 10 - Java
- android - 用于 App 的 Android SharedPreferences 和 IOS 钥匙串是否曾被系统自动删除?
- html - 具有两列和三个卡片 ui 元素的相同高度