html - 无法设置动态创建的 .innerHTML (Angular)
问题描述
我正在尝试更改动态生成的 <'span> 内的文本。<'span> 是在列表 (<'ul><'li>) 中生成的,如果选择更改,则该列表会更改其项目数 (li)。
这是动态列表(有效)
<ul *ngIf="placeholder[0]">
<li *ngFor="let time of placeholder[0], let i = index" >
<div><span id="timer0{{i}}"> lol </span><span *ngIf="timer[0].timeLeft[i] > 0"> : {{timer[0].timeLeft[i]}} secondi <button (click)="restart(0, i)"> reset qua</button><button (click)="check()"> check div</button></span></div>
</li>
</ul>
这是我试图在其中更改文本的 Typescript 代码<span id = "timer0{{i}}">
setTime(i: number){
//setting del timer (azzero tutti i campi)
this.placeholder[i]=[]
this.timer[i].lunghezza = Object.keys(this.datiX.fileJson.ricette[this.datiX.postazioniNome[this.datiX.defPostazione]][this.datiX.ricetta[i]]).length;
for (let j = 0; j < this.timer[i].lunghezza; j++) {
this.placeholder[i][j] = [""]
}
for (let j = 0; j < this.timer[i].lunghezza; j++) {
this.pauseTimer(i, j);
this.timer[i].inizio[j] = 0;
this.timer[i].fine[j] = Object.keys(this.datiX.fileJson.ricette[this.datiX.postazioniNome[this.datiX.defPostazione]][this.datiX.ricetta[i]][j]).length;
this.timer[i].timeLeft[j] = <number> <unknown>Object.keys(this.datiX.fileJson.ricette[this.datiX.postazioniNome[this.datiX.defPostazione]][this.datiX.ricetta[i]][j][0])[this.timer[i].inizio[j]];
}
console.log("check: ",document.getElementById('timer01'))
}
console.log("check: ",document.getElementById('timer01'))在函数内部给了我 = null
,但是如果我调用一个打印它的函数(按下检查按钮后),它会给我正确的值(“哈哈”)...
我做错了什么?
解决方案
推荐阅读
- c++ - 有没有优化两个 BigNum 相乘的好方法?
- docker - 在 Windows 10 Home 的 WSL2 上使用 Docker Desktop 时,如何更改 docker 映像的位置?
- visual-studio - Visual Studio Code 远程开发使 AWS 实例崩溃
- snowflake-cloud-data-platform - 时间旅行和故障安全可以同时申请一个对象吗?
- javascript - 尝试从网络摄像头流中截屏但未捕获流
- xslt - 基于属性值的 XPath
- asp.net-core - 运行“dotnet tool restore”以使“dotnet-ef”命令可用
- go - Golang gRPC 无法保持活动状态:客户端连接正在关闭
- python - 替换文本文件中标记列表的最佳方法
- javascript - 在 MobX 中,为什么渲染函数中的副作用不好