Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用div[ngStyle]作为背景图像,am循环它*ngFor,如果缺少任何图像,如何使用angular 4在其上显示静态图像_Javascript_Angular_Angular2 Directives - Fatal编程技术网

Javascript 使用div[ngStyle]作为背景图像,am循环它*ngFor,如果缺少任何图像,如何使用angular 4在其上显示静态图像

Javascript 使用div[ngStyle]作为背景图像,am循环它*ngFor,如果缺少任何图像,如何使用angular 4在其上显示静态图像,javascript,angular,angular2-directives,Javascript,Angular,Angular2 Directives,如何在没有图像的情况下获取备用图像,我已经尝试使用onError,但它只在图像标签上工作,但这里我只使用div <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.produ

如何在没有图像的情况下获取备用图像,我已经尝试使用onError,但它只在图像标签上工作,但这里我只使用div

<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>
希望这对你有帮助

在div上使用src属性无法显示图像,而且我怀疑当背景图像未加载时是否会触发onError。您是否尝试过console.log?。我猜您需要执行var tmp=new image;tmp.onerror=/*标记为死*;tmp.src=URL;并使用该结果将数组元素更改为默认URL。
<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>