13

ng-src用来加载图像。值是从某个范围变量加载的,如下所示:

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

我的问题是,当我运行时delete $scope.currentReceipt,它使ng-src属性为空,但没有反映在src属性中。因此,我一直在需要空占位符的地方看到该图像。

我该如何处理?

4

4 回答 4

24

这是 ngSrc 和 ngHref 指令的预期行为。这些指令仅支持识别新路径,但当路径不可用时,指令将静默退出(我在这里看到了拉取请求。)。

因此,一种可能的解决方法是在图像变量不再可用时使用 ngShow 和 ngHref 来完全隐藏标签:

<img ng-href="{{currentReceipt.image}}" ng-show="currentReceipt.image" />
于 2013-07-05T16:59:22.967 回答
12

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

于 2014-09-02T11:01:28.327 回答
1

以下解决方案对我有用:

<img ng-src="{{currentReceipt.image}}" ng-show="currentReceipt.image != null" />
于 2015-06-29T23:15:36.553 回答
0

您实际上可以检查长度并执行

 <img ng-show="user.thumbnail.length > 1" class="img-circle thumb pull-left" ng-src="{{user.thumbnail}}" alt="{{user.firstname}} {{user.lastname}}">
于 2016-04-13T16:56:49.227 回答