我正在用斜角书写管道。我在其他页面上有此功能,但是由于某种原因,在这一页上,map()(或也许pipe())运算符不适用于我。由于某些原因,该参数为null。
import { Pipe, PipeTransform } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { map } from 'rxjs/operators';
import { Project } from '../services/project/project.service';
/**
* Project Status Filter Pipe.
* Filters Projects by Status
** Usage: "Projects$ | statusFilter$:filter | async"
*/
@Pipe({
name: 'statusFilter$'
})
export class StatusFilterPipe implements PipeTransform {
transform(items$: BehaviorSubject<Project[]>, ...args: number[]) {
if (items$ === null) { return null; }
const filter = args[0] || null;
if (filter === null) { return items$; }
console.log('status 1', items$); // Behavior Subject - value is array[36]
return items$.pipe(map(
items => {
console.log('status 2', items); // items is NULL
items.filter(p => p.statusId === filter); //throws error
}));
}
}
这就是它的称呼方式:
<ng-container *ngIf="(projects$ | statusFilter$: status.statusId | async) as projects; else projectsLoading">
这是从ngOnInit()事件中调用的用于加载项目$的代码:
private async loadProjects() {
const projects = await this.projectService.listReport('organizations', this.orgId).toPromise();
if (projects) {
projects.sort((a, b) => {
return (a.sortOrder > b.sortOrder) ? 1 : -1;
});
}
this.projects$.next(projects);
}
由于item为null,它将在item.filter()上引发错误。我的36件物品要去哪里?