我想通过它的类循环遍历每个元素,以便在src属性中设置新值。下面是jquery代码,我怎样才能在angular中重写angularcript
$(".image").each(function () {
$(this).attr("src","http://xxx/xxx.png");
});
我想这可能会对你有所帮助:
Array.from(document.getElementsByClassName("image")).forEach(function(item) {
var image_link=item.getAttribute('href');
item.setAttribute('src',"path");
});
尝试使用相反的方法 - 在组件内部存储集合并迭代视图内部:
component.ts
public urls = ['url1', 'url2'];
component.html
<img *ngFor="let url of urls" [src]="url">
如果你现在更改urls
中的某些内容会影响视图。