2017-04-01 80 views
0

我在* ngFor循环中显示图像数组。当我知道<img> ID时,如何更改< img>的src?

itemimg.component.html

<div *ngFor="let itemimg of itemimgs" [class.selected]="itemimg === selectedItemimg" 
      (click)="onSelect(itemimg)"> 
    <img id="{{itemimg.index}}" class="thumb" src="{{itemimg.imageUrl}}" width="{{itemimg.width}}" height="{{itemimg.height}}" 
     style="float: left; margin-right: 3px; margin-bottom: 3px"> 
</div> 

当我点击任何我想要替换第一图像的图像。

itemimg.components.ts(部分)

onSelect(itemimg: Itemimg): void{ 
    this.selectedItemimg = itemimg; 
    var newsrc = "../../assets/images/" + itemimg.route + ".jpg"; 
    //alert (newsrc); 
    *what-goes-here* = newsrc; // the problem 
    } 

我一直在GOOGLE上搜索这个了三个多小时,但无法找到答案。感谢您的期待。

回答

1

请做以下考虑你总是希望更换阵列中的第一个图像:

onSelect(itemimg: Itemimg): void{ 
this.selectedItemimg = itemimg; 
var newsrc = "../../assets/images/" + itemimg.route + ".jpg"; 
//alert (newsrc); 
this.itemimgs[0].imageUrl = newsrc; 

}

+1

当我看到您的回复我很尴尬,因为我应该能够为自己工作,但我确实尝试过。它工作完美,谢谢。 –

0

您想要替换阵列itemimgs中第一个图像的imageUrl

因此,所有你需要的是

this.itemimgs[0].imageUrl = newsrc; 
相关问题