空NG-SRC不更新图像 [英] empty ng-src doesn't update image

查看:198
本文介绍了空NG-SRC不更新图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 NG-SRC 指令,以避免浏览器请求图像前角评价前pression。

I am using the ng-src directive in order to avoid the browser to request the image before Angular evaluate the expression.

NG-SRC = {{图像}} 将更新的的src 的图像的属性如果EX pression形象更改。
我误解了为什么的 NG-SRC 的指令如果EX pression(myImage.png)成为空()。不会更新图像的路径

ng-src={{image}} will update the src attribute of the image if the expression "image" change. I misunderstand why the ng-src directive doesn't update the path of the image if the expression ("myImage.png") become empty ("").

在除权pression成为空时,的 NG-SRC 的属性变成空的,但的的src 的属性仍然是一个已知的的src 的。因此它不更新图像。为什么的的src 的属性不更新(到一个空的的src 的),使图像消失。

When the expression become empty, the ng-src attribute become empty but the src attribute still the last known src. So it doesn't update the image. Why the src attribute isn't updated (to an empty src) so that the image "disappear".

下面是一个 plunker

感谢

推荐答案

这个问题的答案是在 code。这是不是一个错误,这只是他们决定他们想要的行为。启动线13895,就可以看到这个指令code:

The answer to this is in the Angular code. It's not a bug, it's just the behavior they decided they wanted. Starting on line 13895, you can see this directive code:

forEach(['src', 'srcset', 'href'], function(attrName) {
   var normalized = directiveNormalize('ng-' + attrName);
   ngAttributeAliasDirectives[normalized] = function() {
   return {
      priority: 99, // it needs to run after the attributes are interpolated
      link: function(scope, element, attr) {
          attr.$observe(normalized, function(value) {

         if (!value)
             return;

         attr.$set(attrName, value);

         if (msie) element.prop(attrName, attr[attrName]);
       });
     }
    };
  };
});

的关键是:

如果回报;

因此​​,大家可以看到,如果你改变了 NG-SRC 前pression空字符串,这永远不会改变的实际的src 值。你可以解决这个问题做一些MadScone建议。

So as you can see, if you change the ng-src expression to an empty string, this will never change the actual src value. You can get around this doing something MadScone suggested.

这篇关于空NG-SRC不更新图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆