Angular 2 中异步可观察对象和管道上的安全导航运算符

2024-02-09

在异步加载的可观察对象上使用安全导航运算符时,我遇到了将空值(而不是讲座数组)传递到管道的问题:

<div *ngFor="let lecture of ((lecturesObservable | async)?.lectures | lectureType: 'main')" class="list-group-item">

讲座类型.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 中必须忍受的事情吗?


当异步管道的输入可观察值还没有值时,它会根据设计解析为 null。所以,是的,您必须通过设计管道来处理空输入来忍受它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular 2 中异步可观察对象和管道上的安全导航运算符 的相关文章

随机推荐