简体   繁体   中英

using div [ngStyle] for background-image and am looping it *ngFor, if any of image is missing ,how to show static image on it using angular 4

<div class="bg" 
     onError="this.src='https://www.tibs.org.tw/images/default.jpg';" 
     [ngStyle]="{'background-image':  'url(' + imgss.image + ')'}"
     *ngFor="let imgss of prod.product_images | slice:0:1;">
</div>

how to get alternate image if there is no image on it, i have tried using onError but which works only on image tag,but here am using only div.

<div class="bg" [ngStyle]="{'background-image': (imgss.image) ? 'url(' + imgss.image + ')' : 'url(https://www.tibs.org.tw/images/default.jpg)' }"
        *ngFor="let imgss of prod.product_images | slice:0:1;"></div>

Hope this will help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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