3

I am new to Angular (8). I am trying to add background image with condition. Please help me to sort it out. following is my code.

<div class="kt-avatar__holder"
[ngStyle]="{'background-image': if addUser.value.attachmentURL? {{addTDAUser.value.userImage}} else'../assets/media/users/300_20.jpg'}">
  
Content

</div>

2 Answers 2

4

You can use [ngStyle] like this

<div class="kt-avatar__holder" style="height: 200px; width: 200px;"
        [ngStyle]="{'background-image': addUser.value.attachmentURL ? 'url(' + addTDAUser.value.userImage + ')' : 'url(' + '../assets/media/users/300_20.jpg' + ')' }">
       Content
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

it's still not working just vanished all the below form field
Create a stackblitz example.
Its still not work but i found the solution now i am using static background image and for addTDAUser.value.userImage using <img src="addTDAUser.value.userImage "> Vote Up thanks for your help
2
<div class="event-thumbnail"
    [ngStyle]="{'background-image': item.logoURL ? 'url(' + item.logoURL + ')' : 'url(' + 'assets/img/default/event-1.png' + ')' }">

1 Comment

This answer could be greatly improved If you share why you think the code snippet posted solves the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.