AngularJS指令避免在移动设备上呈现图像

时间:2013-10-22 05:47:28

标签: jquery css angularjs twitter-bootstrap

我正在使用Twitter Bootstrap及其网格布局编写AngularJS应用程序。 我喜欢的是编写一个指令,它不仅允许我将display:none设置为移动设备上的图像,而且还可以避免渲染它们(以节省带宽并提高移动设备的速度)。所以我想到了一个类似于检查元素的指令,是否设置了display:none。如果是这样,则应从DOM中删除元素(以及下面的所有元素)。可悲的是,此刻已经渲染了图像。

如何避免在移动设备上呈现图片?该指令迟到了吗?那时dom已经呈现了吗?

电贺 马克

1 个答案:

答案 0 :(得分:0)

我没有时间为你编写指令,但你可以从ngSrc directive source开始,只在某些条件下设置属性... 90%是评论和文档,只有实际上底部有一点代码。我认为这是指令的基本链接功能,normalized类似于ngSrc,相同的代码用于ngHref

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);

      // on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist
      // then calling element.setAttribute('src', 'foo') doesn't do anything, so we need
      // to set the property as well to achieve the desired effect.
      // we use attr[attrName] value since $set can sanitize the url.
      if (msie) element.prop(attrName, attr[attrName]);
    });
  }