离子 2/3: 是否可以隐藏破碎图像的"div"?



我想问一下在离子3中隐藏破碎图像的"div">

我可以使用"onerror"隐藏图像,但div 的位置没有隐藏。这是我的代码:

<div *ngFor="let item of list;">
    <div style="width: calc(100%/2); float:left; position:relative; padding-bottom: calc(100%/2);">
        <img style="object-fit:cover; width: calc(100%); height: calc(100%); padding: 1px; position: absolute;" 
                    [src]="item.img" alt="Norway" 
                    onerror="this.style.opacity='0'" (click)="viewImage(item)"/>
    </div>
</div>

那么,如果图像损坏,是否有任何解决方案可以修复和隐藏div?

错误在 img 标签中不起作用,尝试这样做

<div *ngFor="let item of list;let i = index">
    <div style="width: calc(100%/2); float:left; position:relative; padding-bottom: calc(100%/2);">
        <img style="object-fit:cover; width: calc(100%); height: calc(100%); padding: 1px; position: absolute;"
                    [style.opacity]="error[i] ? 0 : 1"
                    [src]="item.img" alt="Norway" 
                    (error)="error[i] = true" (click)="viewImage(item)"/>
    </div>
</div>

在您的 ts 文件中声明

 error:boolean[] = []

查看演示

最新更新