我有 PrimeNg 表,其中显示了一些数据,包括日期,这是 .html 的相关部分:
<p-table
#dt
[columns]="subjectsColls"
[value]="relatedSubjects"
>
<ng-template pTemplate="header" let-columns>
<tr>
<!-- Here column headers are defined -->
</tr>
<tr>
<ng-container *ngFor="let col of columns" [ngSwitch]="col.field">
<th *ngSwitchCase="'validFrom'">
<p-calendar
[(ngModel)]="dt['filters']['validFrom'] && dt['filters'][col.field].value"
(onSelect)="dt.filter($event, col.field, 'dateFilter')"
(onClearClick)="dt.filter(null, col.field, 'dateFilter')"
(onTodayClick)="dt.filter(currentDate, col.field, 'dateFilter')"
[style]="{ width: '100%' }"
dateFormat="yy-mm-dd"
showButtonBar="true"
[appendTo]="'body'"
></p-calendar>
</th>
</ng-container>
</tr>
</ng-template>
<!-- Here table body is defined -->
</p-table>
这是我在 ngOnInit 中带有自定义过滤器的 .ts 组件:
@Component({...})
export class MyComponent implements OnInit {
@ViewChild('dt', {static: false}) public _table: Table;
ngOnInit() {
FilterUtils['dateFilter'] = (value, filter): boolean => {
console.log(value); // return null and I don't know why
return value.getTime() === filter.getTime();
};
}
}
但是,如果我尝试使用日历中的任何选定日期进行过滤,控制台中会弹出一个错误:
ERROR TypeError: Cannot read property 'getTime' of undefined
at primeng_primeng__WEBPACK_IMPORTED_MODULE_5__.FilterUtils.dateFilter
为什么ngOnInit FilterUtils中的值返回null?