[英]angular2 onError image binding
I'm wondering if we can bind or interpolate the onError on the image tag using angular2,我想知道我们是否可以使用 angular2 绑定或插入图像标签上的 onError,
on the app.component.ts:在 app.component.ts 上:
imageUrl:string;
constructor( ) {
this.imageUrl = 'graphics/placeholder.gif';
}
on the app.component.html:在 app.component.html 上:
<img class="img-responsive" [src]="'graphics/image-1.jpg'" onError="imageUrl"/>
The method below works.下面的方法有效。
<img class="img-responsive" [src]="'graphics/image-1.jpg'" onError="this.src='graphics/placeholder.gif'"/>
But as we using a many images on the app and I would like to make it a simple dynamic solution, I found this answer as well,但是当我们在应用程序上使用了许多图像并且我想让它成为一个简单的动态解决方案时,我也找到了这个答案,
Angular 2 - Check if image url is valid or broken Angular 2 - 检查图像 url 是否有效或损坏
but for some reason is not working, I don't know what I'm doing wrong here但由于某种原因不起作用,我不知道我在这里做错了什么
component.ts组件.ts
onImgError(event){
event.target.src = './assets/imgs/altImg.png'
//Do other stuff with the event.target
}
component.html组件.html
<img [src]="imgUrl" (error)="onImgError($event)">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.