我有一个下拉菜单和一个带有动态文本框的表单。
这是我的下拉菜单,更改后我将其存储到firstYear和secondYear
<select class="form-control" id="year" name="year" [(ngModel)]="yearRange" (change)="onFilter($event)">
<option value='2018-2019'>2018-2019</option>
<option value='2017-2018'>2017-2018</option>
<option value='2016-2017'>2016-2017</option>
</select>
public updateForm: NgForm;
public value_01: any; public value_02: any; public value_03: any;
onFilter(data: any) {
....
yearSplit = this.yearRange.split("-");
this.firstYear = yearSplit[0];
this.secondYear = yearSplit[1];
}
我想发布一个键为countryname_year_1的表格,让我们说所选国家为“印度”,年份为“ 2018”,月份为一月。我试过下面的代码。我在这里工作:
stackblitz.com/edit/angular-mcnxrq
<form name="updateForm" #updateForm="ngForm" (ngSubmit)="submit(updateForm.value)" *ngIf="showEdit" >
<div class="forex">
<table class="table table-bordered">
<tr>
<td>{{firstYear}}</td>
<td><input type="number" step="any" class="form-control" id="{{labelCountry}}_{{firstYear}}_01" name="{{labelCountry}}_{{firstYear}}_01" [(ngModel)]="value_01"></td>
<td><input type="number" step="any" class="form-control" id="{{labelCountry}}_{{firstYear}}_02" name="{{labelCountry}}_{{firstYear}}_02" [(ngModel)]="value_02"></td>
我不确定如何解决此问题。
有人可以帮我吗?
谢谢。
答案 0 :(得分:0)
您将相同的属性用于不同的行,而不是将该属性更改为Object,然后在模板中使用它。
对模板进行一些更改,例如
UserModel.aggregate([
{
$match:{
name:"Alex"
}
},
{
$lookup:{
from:"todomodels",
localField:"$_id",
foreignField:"$owner",
as:"todos"
}
}
])
在模板中
selectedYears: Object = {};
onFilter(data: any) {
....
yearSplit = this.yearRange.split("-");
this.selectedYears[yearSplit[0]]= yearSplit[1];
// this way you don't need to set these to properties
// this.firstYear = yearSplit[0];
// this.secondYear = yearSplit[1];
}
genarateObjectKeys(years: any) {
return Object.keys(years);
}
您可以将模板中的第二年作为<tr *ngFor="let firstyear of genarateObjectKeys(selectedYears)">
<td>{{firstYear}}</td>
<td><input type="number" step="any" class="form-control" id="{{labelCountry}}_{{firstYear}}_01" name="{{labelCountry}}_{{firstYear}}_01" [(ngModel)]="value_01"></td>
<td><input type="number" step="any" class="form-control" id="{{labelCountry}}_{{firstYear}}_02" name="{{labelCountry}}_{{firstYear}}_02" [(ngModel)]="value_02"></td>
</tr>
。
有关在表refer here和this