如何检查网址图片是否损坏

阿里·加桑(Ali Ghassan)

我正在从事ionic 4项目。我的项目是从url获取数据json。我想检查来自url的图像是否损坏。如果坏了,再显示一张图片。我曾尝试过使用不同的代码,但没有人在工作。

我正在使用的代码是

   <ion-row *ngFor="let item of items" justify-content-around test-center>


          <ion-col >
              <img src="/images/data/operators/{{item.flight.airline.code.icao}}_logo0.png"  onerror="this.src='images/data/operators/{{item.flight.airline.code.iata}}_{{item.flight.airline.code.icao}}.png'">

          </ion-col>


        </ion-row>

我什么时候运行出错

ERROR Error: Uncaught (in promise): Error: Template parse errors:
Binding to event property 'onerror' is disallowed for security reasons, please use (error)=...
If 'onerror' is a directive input, make sure the directive is imported by the current module.

根据错误,如果我使用(error)=l得到另一个错误是

ERROR Error: Uncaught (in promise): Error: Template parse errors:
Parser Error: Got interpolation ({{}}) where expression was expected at column 69 in [this.src=

有什么解决办法吗?

Shubham Chaudhary

(错误)捕获由img触发的错误事件。使用类似:

<img src="..." (error)="handleImgError($event, item)">

在您的组件中创建函数handleImgError()

function handleImgError(ev: any, item : any){
   let source = ev.srcElement;
   let imgSrc = `images/data/operators/${item.flight.airline.code.iata}_${item.flight.airline.code.icao}.png`;

   source.src = imgSrc;

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章