Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS doesn't update img src when model changes

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?

like image 205
Sergei Basharov Avatar asked Jul 05 '13 16:07

Sergei Basharov


2 Answers

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

like image 153
Hardik Chauhan Avatar answered Sep 27 '22 20:09

Hardik Chauhan


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" />
like image 29
Stewie Avatar answered Sep 27 '22 22:09

Stewie