13

I use ng-src to load images. Value is loaded from some scope variable, like this:

<img ng-src="{{currentReceipt.image}}"/>

My issue is that when I run delete $scope.currentReceipt, it makes ng-src attribute empty but doesn't reflect it in src attribute. So as a result I keep seeing that image where I need empty placeholder.

How can I deal with it?

1

4 Answers 4

24

This is the expected behaviour from the ngSrc and ngHref directives. These directives only support recognising new paths, but when path is not available, the directives will exit silently (I see a pull request here.).

So a possible workaround could be to use ngShow along with the ngHref to hide the tag altogether when image variable is not available anymore:

<img ng-href="{{currentReceipt.image}}" ng-show="currentReceipt.image" />
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for using ng-show, you may also want to consider removing the image from the DOM with ng-switch if you want it completely removed.
Thanks, seems like I have to workaround it the way you suggest. I have had a look inside Angular code managing ng-src directive and noticed that too, that it just exits, if no value given, not removing existing src value or anything like that. I have decided to use ng-class to manage cases like this. Thanks!
I guess the authors intentionally made the directive to just exit if value is undefined, because removing/clearing the src attribute makes the browser display the broken image thumbnail, which isn't really desired.
12

call $scope.$apply() after delete $scope.currentReceipt.

Comments

1

The following solution works for me:

<img ng-src="{{currentReceipt.image}}" ng-show="currentReceipt.image != null" />

Comments

0

You can actually check for length and do

 <img ng-show="user.thumbnail.length > 1" class="img-circle thumb pull-left" ng-src="{{user.thumbnail}}" alt="{{user.firstname}} {{user.lastname}}">

Comments

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.