RxJS Observables:创造价值

RxJS Observables: Creating with values

我还没有调用这些值的服务,所以我正在使用 aysnc ngFors 编写一些样板代码。

我正在尝试创建一个可被 ngFor 使用的可观察对象。我试试:

 statuses$ = Observable.create((o) => {
    o.next(new NameValue('Open', 'OPEN'));
    o.next(new NameValue('Closed', 'CLOSED'));
    o.complete();
  });

然后

        <mat-option *ngFor="let status of statuses$ | async" [value]="status.value">
          {{ status.name }}
        </mat-option>

但我收到异步错误

找不到 'Open' 类型的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Iterables 比如 Arrays

像这样模拟您的可观察对象:

import { of } from 'rxjs';    

statuses$ = of([new NameValue('Open', 'OPEN'), new NameValue('Closed', 'CLOSED')]);

它给出了一个 *ngFor 可以解释的数组,而不是您当前返回的对象。