简体   繁体   English

来自变量的Ionic3背景图像

[英]Ionic3 background-image from variable

I am using ngFor to output an array of posts. 我正在使用ngFor输出帖子数组。 Each should have a background image. 每个都应具有背景图像。 getBackgroundStyle extracts the URL of the image from the post (which is an array) getBackgroundStyle从帖子中提取图像的URL(它是一个数组)

    <div class="singlePost" *ngFor="let post of data" (click)="itemTapped($event, post)">
        <div style="background-image: url('{{getBackgroundStyle(post)}}')">
            <img src="{{getBackgroundStyle(post)}}">

Now, interestingly after building, the img tag works fine, while the div looses the style tag completely 现在,有趣的是,构建之后,img标签可以正常工作,而div完全松开了样式标签

Output in lab/browser: 实验室/浏览器中的输出:

<div class="singlePost">
    <div>
        <img src="http://theurl.com">

Use attribute binding instead: 使用属性绑定

<div [ngStyle]="{ background-image: 'url(' + getBackgroundStyle(post) + ')' }"></div>

or 要么

<div [style.background-image]="'url(' + getBackgroundStyle(post) + ')'"></div>

And the same should be done with the image element: 并且应该对image元素执行相同的操作:

<img [src]="getBackgroundStyle(post)">

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

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