2017-02-09 50 views
3

我想知道,如果我们可以绑定或使用angular2插补图像标签上的onError,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 - Check if image url is valid or broken

但由于某种原因没有工作,我不知道我在做什么错在这里

回答

5

几乎完成他只是忘记改变事件后的形象。

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

这里是 link