【问题标题】:NG-zorro, problem with iteration of ObservableNG-zorro,Observable 的迭代问题
【发布时间】:2021-10-31 15:11:21
【问题描述】:

我想问你为什么我在 [nzData] 中获取我的 observable 时遇到问题,但如果我在 * nfIF 中获取数据则不是。详细使用这种方法效果很好:

<div *ngIf="visibleAggregate">
  <nz-table
    id="activitiesTable"
    *ngIf="tableData$ | async as Table"
  >
    <thead>
      <tr>
        <th *ngFor="let column of Table.columns">
          {{ column.label }}
        </th>
      </tr>
    </thead>
   
  </nz-table>
</div>

但是如果我将我的 observable 传递给 nzData,它会告诉我它是不可迭代的。我需要将数据传递给 nzData。 ([nzData]="tableData$ | async")

我以这种方式创建 observable


 private groupBySubject = new BehaviorSubject<any>(null);
  private groupBy$ = this.groupBySubject.asObservable();
  tableData$: Observable<TableData> = combineLatest([
    this.database$,
    this.groupBy$,
  ]).pipe(map(([db, sc]) => this.buildTableData(db, sc)));

  group(value: Observable<any>) {
    this.groupBySubject.next(value);
  }

错误:


core.js:6210 ERROR TypeError: listOfData is not iterable
    at MapSubscriber.project (ng-zorro-antd-table.js:1742)
    at MapSubscriber._next (map.js:29)
    at MapSubscriber.next (Subscriber.js:49)
    at CombineLatestSubscriber.notifyNext (combineLatest.js:73)
    at InnerSubscriber._next (InnerSubscriber.js:11)
    at InnerSubscriber.next (Subscriber.js:49)
    at BehaviorSubject._subscribe (BehaviorSubject.js:14)
    at BehaviorSubject._trySubscribe (Observable.js:42)
    at BehaviorSubject._trySubscribe (Subject.js:81)
    at BehaviorSubject.subscribe (Observable.js:28)
    at subscribeToResult (subscribeToResult.js:9)
    at CombineLatestSubscriber._complete (combineLatest.js:52)
    at CombineLatestSubscriber.complete (Subscriber.js:61)
    at Observable._subscribe (subscribeToArray.js:5)
    at Observable._trySubscribe (Observable.js:42)
    at Observable.subscribe (Observable.js:28)

我只是想做:

<div *ngIf="visibleAggregate">
  <nz-table
    #aggregate
    id="activitiesTable"
    [nzData]="tableData$ | async"
  >
    <thead>
      <tr>
        <th *ngFor="let column of aggregate.data">
          {{ column.columns.label }}
        </th>
      </tr>
    </thead>
  </nz-table>
</div>

我已经将这种方法与其他 Obserables 一起使用,并且它可以工作,甚至从文档中也是如此。 我不明白为什么这个 obserable 只能在显示的第一种模式下工作。

【问题讨论】:

  • 应该也写错了。
  • 好的,抱歉,完成了!

标签: angular typescript rxjs observable ng-zorro-antd


【解决方案1】:

如果tableData$BehavourSubject 并且您最初为其提供默认值,[nzData]="tableData$ | async" 将起作用。

这里的 async 只是意味着它会更新并返回 nzData 那个 observable 里面的值。 如果没有异步,您将传递 arr: any[] = new Subject()new Subject() 不是数组或可迭代的。

*ngIf=" only checked if the statement here is true or false "

由于tableData$ 不是未定义的,它会因主题而返回true,而不是在observable 中提供的值。

\\ initialize with  empty array so its always have a value
    tableData$ = new BehaviorSubject<Table[]>([]) 
    
     combineLatest([
        this.database$,
        this.groupBy$,
      ]).subscribe(([db, sc]) => this.tableData$.next(this.buildTableData(db, sc)));

【讨论】:

  • 我需要在不使用 ngIf 的情况下执行 [nzData] = "tableData $ | async" 来获取数据。
  • 你能更新一下你在哪里使用过 nzData 的问题吗,我没有看到它被使用
  • 阅读我的答案,tableData$ 必须是 behaviorSubject,它没有 BehavourSubject([]) 的初始化值。
  • tableData$ = new BehaviorSubject([]) combineLatest([ this.database$, this.groupBy$, ]).subscribe(([db, sc]) => this.tableData $.next(this.buildTableData(db, sc)));
猜你喜欢
  • 2020-11-26
  • 2020-02-08
  • 2020-03-01
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 2021-10-06
相关资源
最近更新 更多