【发布时间】:2025-12-07 11:25:01
【问题描述】:
这是我的“monthlyCalendar”对象:
{
"monthName": "September 2019",
"dateObjList": {
"1": {
"dayOfWeek": "0",
"isPublicHoliday": false,
.............
},
"2": {
"dayOfWeek": "0",
"isPublicHoliday": true,
.............
},
.....................
}
}
这是我的组件.ts:
...........
ngOnInit() {
this.monthlyCalendarService.getMonthlyCalendar(null, null).subscribe(
(res: MonthlyCalendar) => {
this.monthlyCalendar = res;
this.monthName = this.monthlyCalendar.monthName;
},
(error: Error) => {
alert('Something wrong when getting Monthly Calendar data.\n' + error.message);
},
);
}
.........
这是我每月的CalendarService.ts:
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { MonthlyCalendar } from './monthly-calendar';
import { Observable } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class MonthlyCalendarService {
constructor(private http: HttpClient) { }
getMonthlyCalendar(year: string, month: string): Observable<MonthlyCalendar> {
const params = new HttpParams();
params.set('year', year);
params.set('month', month);
return this.http.get('backend/getMonthlyCalendar.php', {params}).pipe(map((res: MonthlyCalendar) => res));
/*
return this.http.get('backend/getMonthlyCalendar.php', {params})
.pipe(map((res: MonthlyCalendar) =>{ console.log('service:' + JSON.stringify(res)); return res; })
//, catchError(this.handleError)
);
*/
}
}
这是我的component.html:
...........
<td *ngFor="let dateObj of monthlyCalendar?.dateObjList|async" class="phCell">
<span *ngIf="dateObj.isPublicHoliday">PH</span>
</td>
...........
我想在 dateObj.isPublicHoliday 属性为 true 时显示 span 标签。
但是,浏览器提示我以下错误消息:
ERROR Error: InvalidPipeArgument: '[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]' for pipe 'AsyncPipe'
如果我也将“keyvalue”管道应用于 *ngFor,即
<td *ngFor="let dateObj of monthlyCalendar?.dateObjList|async|keyvalue" class="phCell">
我收到以下错误消息:
Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'
我怎样才能让它工作?
【问题讨论】:
标签: angular