使用ng

时间:2017-06-13 16:13:12

标签: javascript css json angular

我的背景图片网址有问题。 我有阵列有图像网址我怎么能在背景图片网址中使用它

<div class="col-lg-3 col-md-3 col-sm-6" *ngFor="let course of courses"><a>
    </a><div class="box"><a>
        <div class="box-gray aligncenter"  [style.backgroundColor]="course.imageUrl" >
        </div>
        </a><div class="box-bottom"><a >
            </a><a >{{course.name}}</a>
        </div>
    </div>
</div>

4 个答案:

答案 0 :(得分:37)

请参阅此Angular2 dynamic background images

// inappropriate style.backgroundColor 
[style.backgroundColor]="course.imageUrl"

// style.backgroundImage
[style.backgroundImage]="'url('+ course.imageUrl +')'"

答案 1 :(得分:7)

您应该使用background代替backgroundColor

[style.background]="'url('+course.imageUrl+')'"

答案 2 :(得分:7)

谢谢你的回答, 正确的代码是

[ngStyle]="{'background-image':'url(' + course.imageUrl + ')'}">

答案 3 :(得分:1)

您可以通过在单个变量中添加网址路径来实现。 例如

tobor

&str
bgImageVariable="www.domain.com/path/img.jpg";