【发布时间】:2018-05-02 06:27:46
【问题描述】:
我有一个模板,里面有多个图像(img)标签,所有这些都在 foreach 循环中迭代并在 typescript 文件中修改这个 img src 属性/属性。
HTML 代码:
<div *ngFor="let lt of list">
<div>
<a href="{{lt.url}}" target="_self">
<img mat-card-image name="{{lt.name}}" class="image" src="" placement="top" container="body">
</a>
</div>
<div>
<a href="{{lt.url}}" target="_self">
<img mat-card-image name="{{lt.name}}" class="image" src="" placement="top" container="body">
</a>
</div>
<div>
<a href="{{lt.url}}" target="_self">
<img mat-card-image name="{{lt.name}}" class="image" src="" placement="top" container="body">
</a>
</div>
</div>
打字稿文件中的代码,
ngOnInit() {
for (const lt of list) {
if (lt.title === 'xyz') {
lt.title = 'XYZ';
}
if (lt.appFlag) {
this.getIcon(lt.name);
}
}
}
private getIcon(name: string): void {
const imageUrl = this.service.createURL('abc');
// how to get img tag using property/attribute name(name here is unique item name) from HTML without using document.querySelector etc.
....
....
//const img = this.name.nativeElement.name;
//console.log('inside getIcon:img', img);
if (img) {
img['src'] = imageUrl;
}
});
}
【问题讨论】:
-
不使用 document.querySelector 或 document.getElementByName 等,是否可以使用 ElementREf 等任何 Angular 功能
-
这对你有用吗???
标签: angular typescript angular5 angular2-template