angular - 所选选项的角度显示值
问题描述
我想将所选选项的值显示到我的表中:
HTML 文件:
<div class="liste">
<select class="form-control" name="Container" size="5" (change)="selectChangeHandler($event)">
<option *ngFor="let v of values" [value]="v">{{v.Name}}
</select>
</div>
<div class="tableau">
<table>
<tr align="center">{{v.Name}}</tr>
<tr>
<td>Matricule: {{v.Matricule}}</td>
</tr>
</table>
</div>
.ts 文件:
values = [
{ Name: "Container A", Matricule:"ABC" },
{ Name: "Container B", Matricule:"BCD" },
{ Name: "Container C", Matricule:"CDE" },
{ Name: "Container D", Matricule:"DEF" },
{ Name: "Container E", Matricule:"EFG" },
{ Name: "Container F", Matricule:"FGH" },
];
解决方案
首先,选项中的 [value] 不应该是您将其与对象绑定的对象。尝试关注以下
selectedValue = {
Name: '',
Matricule: ''
}
values = [{
Name: "Container A",
Matricule: "ABC"
},
{
Name: "Container B",
Matricule: "BCD"
},
{
Name: "Container C",
Matricule: "CDE"
},
{
Name: "Container D",
Matricule: "DEF"
},
{
Name: "Container E",
Matricule: "EFG"
},
{
Name: "Container F",
Matricule: "FGH"
},
];
selectChangeHandler(event) {
this.selectedValue = this.values[event.target.value];
}
<select class="form-control" name="Container" (change)="selectChangeHandler($event)">
<option *ngFor="let v of values;let i = index" [value]="i">
{{v.Name}}
</option>
</select>
<table>
<tr align="center">{{selectedValue.Name}}</tr>
<tr>
<td>Matricule: {{selectedValue.Matricule}}</td>
</tr>
</table>
推荐阅读
- c# - 调试 .net Core IModelBinder 有 Visual Studio 跳线
- linux - 如何从 Linux 上的 Windows 注册表中检索区域设置?
- java - Intellij 打不开我的 JDK 所在的文件夹
- php - PHPUnit 错误“无法打开流:没有这样的文件或目录”
- android - com.android.chrome 导致的崩溃
- laravel - 如何将路由变量值传递给模型类方法
- c++ - 与 C++ 中的循环和变异向量混淆
- javascript - 浏览器中 Javascript 源代码中的“w()”、“n()”
- python - Pandas 基于模式长度的阈值数据序列
- r - 嵌套 rollmean - 如何避免来自每个 DF 边缘的 NA