我在一个time
JSON数组中有多个data
项,我想用 Moment.js 对其进行解析和格式化并将其存储到this.appointmentTimes
中数组,我可以使用{{appointmentTime.time}}
在视图上轻松访问它。
这是我的JSON:
[
{ "time":"2018-10-22T14:30:00+0200", "slotsAvailable":1 },
{ "time":"2018-10-22T14:45:00+0200", "slotsAvailable":1 },
{ "time":"2018-10-22T15:00:00+0200", "slotsAvailable":1 }
]
页面刷新后,我收到此错误:
ERROR in src/app/pages/booking/form/booking.component.ts(75,9): error TS2322: Type 'string' is not assignable to type 'object[]'.
src/app/pages/booking/form/booking.component.ts(76,22): error TS2345: Argument of type 'object[]' is not assignable to parameter of type 'string'.
这是我的相应功能,将通过我的视图执行。
private appointmentLocations: Array<object> = [];
private appointmentTypes: Array<object> = [];
private appointmentTimes: Array<object> = [];
onChangeTypeId(TypeId) {
console.log(TypeId);
this.selectedAppointmentTypeId = TypeId;
this.apiService
.getAppointmentTimesById(
this.selectedAppointmentTypeId,
this.selectedAppointmentLocation,
this.selectedDatePicker
)
.subscribe((data: Array<object>) => {
/*this.appointmentTimes = data; */
this.appointmentTimes = JSON.stringify(
JSON.parse(data).map(function(data) {
var pattern = 'HH:mm';
data = moment(data.time).format(pattern);
return data;
})
);
console.log(data);
});
}
}
答案 0 :(得分:0)
.map(response => response.json())
,因为响应是字符串,而不是JSON。