访问元素表有这样一个我如何通过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