訪問元素表有這樣一個我如何通過ID在角2
<table>
<tbody>
<tr *ngFor="let row of createRange(seats.theatreDimension.rowNum)">
<td id={{row}}_{{seat}} *ngFor="let seat of createRange(seats.theatreDimension.seatNumInRow)">
</td>
</tr>
</tbody>
</table>
我想從角2周的.ts文件訪問表TD元素。下面是功能:
ngOnInit() {
this.getSeats();
}
getSeats() {
this.cinemaService.getAllSeats(this.repertoire.id).subscribe(
data => {
this.seats = data.json();
this.setReservedSeats();
}
)
}
setReservedSeats() {
this.seats.reservedSeats.forEach(seat => {
let cssSeatId = seat.rowNumReserved + "_" + seat.seatNumInRowResereved;
document.getElementById(cssSeatId).className += "reserved";
}
)
}
之後,我想動態設置每TD的類,但我在我的瀏覽器中獲取此控制檯錯誤:
錯誤類型錯誤:無法讀取屬性'className'爲空
只需再次注意我動態生成td ID。它們的格式爲rowNum_cellNum。
我正在從API這個對象。
{
"theatreDimension": {
"rowNum": 17,
"seatNumInRow": 20
},
"reservedSeats": [
{
"rowNumReserved": 9,
"seatNumInRowResereved": 19
},
{
"rowNumReserved": 2,
"seatNumInRowResereved": 4
},
{
"rowNumReserved": 15,
"seatNumInRowResereved": 15
}
]
}
我使用theatreDimension做表。然後,我儘量讓從reservedSeats陣列保留席位與紅色背景(保留)
如何我可以從角2訪問TD元素和解決這個問題?
代碼中'cssSeatId'的值是多少?你有檢查嗎? – Niladri
在此的forEach我正確地形成cssSeatId,所以在形式上rowNum_seatNum:9_19,2_4,15_15。即與我通過api – Aleksandar