我正在使用的代码是
<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)=
我又遇到另一个错误是
ERROR Error: Uncaught (in promise): Error: Template parse errors:
Parser Error: Got interpolation ({{}}) where expression was expected at column 69 in [this.src=
有什么解决办法吗?
答案 0 :(得分:2)
(错误)捕获由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;
}
答案 1 :(得分:2)
您应该尝试一下。如果找不到图片,则会加载备用图片
<img class="img-style" [src]="user.photo ? user.photo : 'https://images.vexels.com/media/users/3/145908/preview2/52eabf633ca6414e60a7677b0b917d92-male-avatar-maker.jpg'">