简体   繁体   English

当模型更改时,AngularJS不会更新img src

[英]AngularJS doesn't update img src when model changes

I use ng-src to load images. 我使用ng-src加载图像。 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. 我的问题是,当我运行delete $scope.currentReceipt ,它使ng-src属性为空,但不会在src属性中反映出来。 So as a result I keep seeing that image where I need empty placeholder. 因此,我一直看到我需要空占位符的图像。

How can I deal with it? 我怎么处理它?

This is the expected behaviour from the ngSrc and ngHref directives. 这是ngSrc和ngHref指令的预期行为。 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: 因此,当图像变量不再可用时,可能的解决方法是使用ngShow和ngHref来完全隐藏标记:

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

在删除$ scope.currentReceipt之后调用$ scope。$ apply()。

以下解决方案适合我:

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

你可以实际检查长度和做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM