angular - 如何根据文本框的输入生成表格的动态行
问题描述
我正在解决一个问题,我需要根据用户通过文本框给出的输入生成表的动态行。
我的 .ts 代码:
start(val){
this.printVal=val;
console.log('Value of start is',this.printVal);
return new Array(val);
}
.html 代码:
<div class="container col-lg-12">
<input type="number" #data>
<button (click)="start(data.value)">Start</button>
<br><br>
<table>
<ng-container >
<tr *ngFor="let item of [].constructor(printVal); let i = index"> //If instead of printVal, I give a number then it prints the desired output
<td>{{i}}</td>
</tr>
</ng-container>
</table>
</div>
实际上我需要创建一个这样的表
| Input|sec|Multipilcation
|------|---|------------------------------
|10 |1 |10
|10 |2 |20
|10 |3 |30
|10 |4 |40
.
.
.
10 |10 |100
其中 10 是通过输入框提供的数字,每秒后应添加一行,其值应打印,第三列乘以两者,表格行应继续,直到提供的输入值。
请告诉我我做错了什么以及如何根据提供的输入打印动态行。
解决方案
printVal
是一个字符串变量,所以当你像这样创建一个新数组时:
[].constructor(printVal)
例如[].constructor("3")
,您将得到一个包含一个元素的数组。
您应该将值从输入转换为数字:
this.printVal = +val;
推荐阅读
- c# - 如何对使用 Response.OnStarting 的 .NET 中间件进行单元测试
- c++ - 在 C++ 中从 MYSQL 存储和检索 BYTE* 数据
- c# - Blazor(客户端)StateHasChanged()不更新页面
- google-apis-explorer - 错误 500:请求的资源太大而无法返回,responseTooLarge
- java - 覆盖 DateTimeFormatter 本地化的世纪日期样式解析策略
- keycloak - 通过代理或直接从单页应用程序访问 Keycloak Admin API?
- css - 如何从输入中清除边距(引导程序)?
- python - 如何在命令行中自动运行输入/输出
- python - 我可以让一个函数在一个列表中返回多个值吗?
- python - 从python中的字符串中间删除逗号