如何检查 url 中的图像是否损坏

Ali*_*san 4 angular ionic4

我正在从事 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>
Run Code Online (Sandbox Code Playgroud)

我在运行时遇到错误

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.
Run Code Online (Sandbox Code Playgroud)

取决于错误,如果我使用(error)=我得到另一个错误是

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

请问有什么解决办法吗?

Shu*_*ary 5

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

<img src="..." (error)="handleImgError($event, item)">
Run Code Online (Sandbox Code Playgroud)

在您的组件中创建函数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;

}
Run Code Online (Sandbox Code Playgroud)