【问题标题】:Update images created by ng-repeat using directive使用指令更新由 ng-repeat 创建的图像
【发布时间】:2014-11-17 20:02:24
【问题描述】:

我使用 sockte.io 从服务器获取我的数据,并且取决于数据(item.text)我必须更改图像。 我尝试访问 DOM 并使用指令修改图像,但没有成功。

这是我的代码:

HTML:

<table>
    <tr ng-repeat="item in items"  myDirective>
        <td><img  ng-src="{{myImages}}" ></td> <td>{{item.text}}</td>
    </tr>

</table>

JS:

App.directive('myDirective', function(){
  return {
    link: function($scope, elm){
      console.log('eee', elm);
      elm[0]['children'][0]['children'][0].attr('src',"/url/to/the/image");
    }
  }
});

我该如何解决这个问题,或者是否有更好的方法来解决这个问题。

更新:

在给定时间图像不相同,我想使用它们的索引访问每个图像的索引。

更新: 我添加了这个example,我该如何修改例如具有'text 2'的图像

【问题讨论】:

  • 在你的小提琴中只有一个图像地址:http://www.exlibrisgroup.com/fr/files/Products/question_icon.gif 那应该是一个数组吗?
  • 这不是我的全部代码,我已经更新了示例(只是我添加了一个新图像),我想用这个图像只更新“文本 2”。我该怎么做?
  • 这肯定会有所帮助。谢谢

标签: javascript angularjs angularjs-ng-repeat angular-directive


【解决方案1】:

您不应该为此需要指令。使用ng-src 而不是普通的图像src 属性。

【讨论】:

  • 在给定时间图像不一样,ng-src可以吗?
  • 如果您的意思是 ng-src="myImages" 中的 myImages 变量在其他地方更新,那么是的,这是可能的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
相关资源
最近更新 更多