如何在Angular 2 ngFor Loop中使用本地引用获取元素的值?
请参阅下面的代码:
component.html
<tbody>
<tr *ngFor="let storages of storageId; let i = index" (click)="storageSelected(i, storageWasSelected)">
<td #storagewasselected>{{ storages.id }}</td>
<td>{{ storages.storagetype }}</td>
<td>{{ storages.islock }}</td>
<td>{{ storages.createdby }}</td>
<td>{{ storages.createddate }}</td>
</tr>
</tbody>
component.ts
@ViewChild('storagewasselected') storageWasSelected;
storageSelected(index: number, storageWasSelected){
this.storageEntryService.StartEditStorage.next(index);
this.selectedStorageId = storageWasSelected.innerHTML;
console.log(this.selectedStorageId);
}
答案 0 :(得分:0)
您可以直接在storageIndex()方法上传递索引,而不是使用ViewChild
(click)="storageSelected(storages.id)"
另外,你不需要这个
#storagewasselected
在你的模板上。