簡體   English   中英

當模型更改時,AngularJS不會更新img src

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

我使用ng-src加載圖像。 值從某個范圍變量加載,如下所示:

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

我的問題是,當我運行delete $scope.currentReceipt ,它使ng-src屬性為空,但不會在src屬性中反映出來。 因此,我一直看到我需要空占位符的圖像。

我怎么處理它?

這是ngSrc和ngHref指令的預期行為。 這些指令僅支持識別新路徑,但是當路徑不可用時,指令將以靜默方式退出 (我在此處看到了拉取請求。)。

因此,當圖像變量不再可用時,可能的解決方法是使用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