Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 4 style background image when using ngFor

I have problem with background image url. i have array have images url how can I use it in background image url

<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>
like image 826
eslam masoud Avatar asked Jun 13 '17 16:06

eslam masoud


4 Answers

Refer to this one Angular2 dynamic background images

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

// style.backgroundImage
[style.backgroundImage]="'url('+ course.imageUrl +')'"
like image 145
Hitmands Avatar answered Oct 24 '22 00:10

Hitmands


thank you for your answers, the correct code is

[ngStyle]="{'background-image':'url(' + course.imageUrl + ')'}">
like image 23
eslam masoud Avatar answered Oct 24 '22 00:10

eslam masoud


You should use background instead of backgroundColor

[style.background]="'url('+course.imageUrl+')'"
like image 11
Pankaj Parkar Avatar answered Oct 24 '22 00:10

Pankaj Parkar


you can do it by adding url path in a single variable. for example

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

and

second way
[ngStyle]="{'background-image': 'url(' + bgImageVariable + ')'}"
like image 4
Coder Girl Avatar answered Oct 24 '22 02:10

Coder Girl