0
試圖從*ngFor
獲得attr.data-item-code
選擇選項,我想要獲取數據屬性值,當我點擊一個按鈕。Angualr2如何獲取attr.data-item-code on(點擊)
<div>
<select>
<option *ngFor="let x in players" [attr.data-item-code]="x.code">{{ x.name }}</option>
</select>
<button (click)="preview()">Preview Player</button>
</div>
我想是這樣,但低於該值null
<div #dt>
<select>
<option *ngFor="let x in players" [attr.data-item-code]="x.code">{{ x.name }}</option>
</select>
<button (click)="preview(dt.getAttribute('data-item-code'))">Preview Player</button>
</div>
預覽功能: -
preview(code){
console.log(code) // returns null
}
有沒有辦法讓x.code
當我點擊preview()
?
我已經這樣做了,以獲得玩家的名字。 – MrNew
@MrNew,那麼最新的問題 – Satpal
啊nvm是-.-弄糊塗了,我可以通過'x'來獲取整個對象。 – MrNew