gpt4 book ai didi

angular - 在日历的对象数组上使用 ngFor

转载 作者:行者123 更新时间:2023-12-05 08:31:51 30 4
gpt4 key购买 nike

我如何使用 ngFor 循环遍历以下对象数组并仅获取月份的名称?

{
"Jan": {
"name": "January",
"short": "Jan",
"number": 1,
"days": 31
},
"Feb": {
"name": "February",
"short": "Feb",
"number": 2,
"days": 28
},
"Mar": {
"name": "March",
"short": "Mar",
"number": 3,
"days": 31
},
"Apr": {
"name": "April",
"short": "Apr",
"number": 4,
"days": 30
},
"May": {
"name": "May",
"short": "May",
"number": 5,
"days": 31
},
"Jun": {
"name": "June",
"short": "Jun",
"number": 6,
"days": 30
},
"Jul": {
"name": "July",
"short": "Jul",
"number": 7,
"days": 31
},
"Aug": {
"name": "August",
"short": "Aug",
"number": 8,
"days": 31
},
"Sep": {
"name": "September",
"short": "Sep",
"number": 9,
"days": 30
},
"Oct": {
"name": "October",
"short": "Oct",
"number": 10,
"days": 31
},
"Nov": {
"name": "November",
"short": "Nov",
"number": 11,
"days": 30
},
"Dec": {
"name": "December",
"short": "Dec",
"number": 12,
"days": 31
}
}

这是我到目前为止所做的:

<option *ngFor="let date of dates" 
[ngValue]="date.name" [(ngModel)]="dobMonth">{{ date.name }}</option>

结果:

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

最佳答案

使用|键值管道!

angular 6.0.0 以后 | keyvalue 管道添加到 *ngFor 中的渲染对象。

<option *ngFor="let date of dates | keyvalue" 
[ngValue]="date.name" [(ngModel)]="dobMonth">{{ date.name }}</option>

关于angular - 在日历的对象数组上使用 ngFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54953257/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com