2017-02-09 52 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