繁体   English   中英

angular2 onError 图像绑定

[英]angular2 onError image binding

我想知道我们是否可以使用 angular2 绑定或插入图像标签上的 onError,

在 app.component.ts 上:

imageUrl:string;

  constructor( ) {
      this.imageUrl = 'graphics/placeholder.gif';
   }

在 app.component.html 上:

<img class="img-responsive" [src]="'graphics/image-1.jpg'" onError="imageUrl"/>

下面的方法有效。

<img class="img-responsive" [src]="'graphics/image-1.jpg'" onError="this.src='graphics/placeholder.gif'"/>

但是当我们在应用程序上使用了许多图像并且我想让它成为一个简单的动态解决方案时,我也找到了这个答案,

Angular 2 - 检查图像 url 是否有效或损坏

但由于某种原因不起作用,我不知道我在这里做错了什么

几乎完成了,他只是在活动结束后忘记改变形象。

errorHandler(event) {
   console.debug(event);
   event.target.src = "https://cdn.browshot.com/static/images/not-found.png";
}

这是链接

组件.ts

onImgError(event){
 event.target.src = './assets/imgs/altImg.png'
//Do other stuff with the event.target
}

组件.html

 <img [src]="imgUrl" (error)="onImgError($event)">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM