【问题标题】:RxJS Observables: Creating with valuesRxJS Observables:用值创建
【发布时间】:2020-08-27 17:03:54
【问题描述】:

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

我正在尝试创建一个可以被 ngFor 使用的 oberable。我试试:

 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

【问题讨论】:

  • subscribe 返回Subscription 对象,不可观察。
  • 你为什么要订阅 使用异步管道?
  • 错误地添加了订阅,但仍然失败。嗯。

标签: angular rxjs rxjs-observables


【解决方案1】:

像这样模拟你的 observable:

import { of } from 'rxjs';    

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-10
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 2016-12-03
    相关资源
    最近更新 更多