Attribute property binding for background-image url in Angular 2

落爺英雄遲暮 提交于 2019-11-28 07:20:11
Thierry Templier

I think that you should use something like that:

<div class="profile-image"
     [ngStyle]="{ 'background-image': 'url(' + image + ')'}">

where image is a property of your component.

See this question:

redfox05

You don't need to use NgStyle. You can also do this:

[style.background-image]="'url(' + image + ')'"

See more at How to add background-image using ngStyle (angular2)?

The main reason is very simple you declared global variable as blankImage but in your template you typed image . thats two different variable

your ts code **blankImage**: string = '../assets/.../camera.png';

your template code **<div class="profile-image" [ngStyle]="{'background-image': 'url(' + **image** + ')'}"></div>.`

just change the image to blankImage or viceversa

that's wrong

I think you should add:

<div class="profile-image" [ngStyle]="{ 'backgroundImage': url({{image}})">

Regards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!