【发布时间】:2018-07-19 14:39:14
【问题描述】:
我希望下拉菜单显示我的数据中的 2017 年和 2018 年。 2017 年和 2018 年在我的 json 数据文件中重复了很多。但我希望在选择时显示所有 2017 年数据,并在选择时显示所有 2018 年数据。目前它显示所有数据并且下拉列表被过度填充。
这是下拉菜单的 Html 代码:
<div class="row justify-content-center">
<div class="col-4s">
<p>Financial Year:</p>
</div>
<div class="col-4s">
<select>
<option *ngFor="let volumes of volumes">{{ volumes.month |
date: 'yyyy' }}</option>
</select>
</div>
</div>
Angular4 代码: 导出类 VehicleVolumeEditComponent 实现 OnInit {
volumes: Volumes[];
groupedVolumes : any;
constructor(private volumeService: VolumeService, private router: Router) {
this.volumeService.getVolumes().subscribe(volumes => {
this.volumes = volumes;
this.groupedVolumes = this.group(this.volumes);
this.dataOk = true;
}
}
json 文件:
[
{
"id": 1,
"month": "2017-03-01"
}
{
"id": 2,
"month": "2017-04-01"
}
{
"id": 3,
"month": "2017-05-01"
}
{
"id": 4,
"month": "2017-06-01"
}
{
"id": 5,
"month": "2017-07-01"
}
{
"id": 6,
"month": "2017-08-01"
}
{
"id": 7,
"month": "2017-09-01"
}
{
"id": 8,
"month": "2017-10-01"
}
{
"id": 9,
"month": "2017-11-01"
}
{
"id": 10,
"month": "2017-12-01"
}
{
"id": 11,
"month": "2018-01-01"
}
{
"id": 12,
"month": "2018-02-01"
}
{
"id": 13,
"month": "2018-03-01"
}
]
【问题讨论】:
-
您应该在 component.ts 文件的一个变量中维护唯一年份数组并使用它
-
问题是什么时候连接到数据库,那些年会一直在变化。所以需要从json数据中填充
-
请发布您的 component.ts 代码和卷的结构
-
我已经添加了我的 component.ts 代码和我的卷文件
标签: arrays json angular dropdown