2

I have the following code that shows the user avatar:

<div class="dashboard_image" style="background-image:url({{(user.avatar!=null)?user.avatar:'/img/general_user.svg'}})" style="width:200px;height:200px"></div>

If user do not have any image, user.avatar comes null.

In Chrome works fine but not in IE 11. What's wrong?

2
  • Have u checked that if your html document is properly rendered (whether the style of that element holds the right value)?After reading your comments, I doubt your issue may be not at the angular side but have something to do with the SVG display problem in IE 11. Commented Aug 9, 2016 at 12:41
  • @MMhunter I thought so but in header, the SVG is shown perfectly. That's the strange thing! Commented Aug 9, 2016 at 16:30

2 Answers 2

3

Try using ng-style, which exists for this purpose.

https://docs.angularjs.org/api/ng/directive/ngStyle

<div
    class="dashboard_image"
    style="width:200px;height:200px"

    ng-style="{
        backgroundImage: 'url(' + (user.avatar!=null ? user.avatar : '/img/general_user.svg') + ')'
    }">
</div>

(not tested)

Sign up to request clarification or add additional context in comments.

8 Comments

It arises an error: angular.js:13550 Error: [$parse:syntax] errors.angularjs.org/1.5.5/$parse/……_user.svg%20%2B%20')'%20%7D&p4=%2Fimg%2Fgeneral_user.svg%20%2B%20')'%20%7D
Lose the {{ and }} curly braces from the ng-style expression: { backgroundImage: 'url(' + (user.avatar!=null) ? user.avatar : '/img/general_user.svg)' }
Now error free but do not show the image in neither case.
I changed it a little: ng-style="{ 'background-image': 'url(' + (user.avatar!=null ? user.avatar : '/img/general_user.svg') + ')' }". Now it works.
I altered it even further. Like I said - I did not test it and obviously it was a mistake :))
|
0

You should do something like this:

<div class="dashboard_image" ng-style="{'background-image': user.avatar!=null ? 'url(user.avatar)' : 'url(/img/general_user.svg)'" style="width:200px;height:200px"></div>

1 Comment

The main issue still remains. In case there is no avatar, the default image is not shown in IE11, but in Chrome yes.

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.