angular - Angular 8 - How to resolve 'ExpressionChangedAfterItHasBeenCheckedError'
问题描述
I have a component that uploads files to a database. Whilst the files are uploading I display an overlay with the amount of files selected, and the amount of files successfully uploaded.
I wanted the overlay to automatically close when the amount of files uploaded is equal to the amount of files selected. However, my implementation has resulted in the following:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: true'. Current value: 'ngIf: false'
When files are selected for upload, I set overlay_uploadImage
to true
. That will display the overlay. Inside the overlay I have the following div. My intention here was that when the upload count is equal to the amount of files selected for upload, then call the overlay_uploadImage
function. This function will set overlay_uploadImage
to false
and therefore hide the overlay container.
<div *ngIf="uploadCount == files.length?overlay_uploadImage:false"></div>
Function called when criteria is met
overlay_imageUploads(){
this.files = [];
this.overlay_uploadImage = false;
}
解决方案
当角度检查一个值然后值发生变化时会出现此错误,它不是“错误”,因为它不会中断
我建议您使用不同的语法:
<div *ngIf="overlay_UploadImage && uploadCount === files.length"></div>
推荐阅读
- java - 将子类型集合分配给父类型
- node.js - 通过节点 slushjs 连接到特定的 MarkLogic 数据库
- javascript - 为什么这个javascript不能在html中工作,而是在控制台中工作?
- jakarta-ee - 跨各种 Bean 的 Java EE 事务类型
- python - 如何通过终端缩进多个 JSON 文件
- javascript - 通过单击验证按钮检查表中 url 列的验证
- xml - 构建具有未知复杂类型的 xml 模式
- java - Spring将单例bean注入原型bean导致单例重新创建
- javascript - 显示模态后停止触发 settimeout
- sql-server - datetime 参数加 1:操作数类型冲突:datetime2 与 int 不兼容。修复代码,而不是 SQL?