Angular 2中异步可观察和管道上的安全导航操作员 [英] Safe navigation operator on an async observable and pipe in Angular 2
问题描述
在异步加载的可观察对象上使用安全导航运算符时,我遇到了将空值(而不是讲座数组)传递给管道的问题:
I'm running into a problem with a null value (instead of an array of Lectures) being passed to a pipe when using the safe navigation operator on an async loaded observable:
<div *ngFor="let lecture of ((lecturesObservable | async)?.lectures | lectureType: 'main')" class="list-group-item">
lecture-type.pipe.ts
lecture-type.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
import { Lecture } from './lecture';
@Pipe({name: 'lectureType'})
export class LectureTypePipe implements PipeTransform {
transform(allLectures: Lecture[], lectureType: string): Lecture[]{
return allLectures.filter(lecture => lecture.type==lectureType);
}
}
一旦异步加载了这些讲义,就不用管道就可以反复进行这些讲义.这只是我在ng2中必须忍受的东西吗?
The lectures are iterated through fine without the pipe, once they are loaded by async. Is this just something I have to live with in ng2?
推荐答案
当异步管道的输入可观察到的值尚无值时,该管道将根据设计解析为null.所以是的,您将不得不通过设计管道来处理空输入来忍受它.
The async pipe resolves to null by design when its input observable does not yet have a value. So yes, you will have to live with it, by designing your pipe to handle a null input.
这篇关于Angular 2中异步可观察和管道上的安全导航操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!