我想问你为什么我在nzData中获取我的可观察值有困难,但如果我在* 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")
我以这种方式创建了可观察对象
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>
我已经将这种方法用于其他可观察性,甚至在文档中也是有效的。我不明白为什么这个obserable只能在显示的第一种模式下工作。
发布于 2021-09-01 22:51:00
如果tableData$
为BehavourSubject
,并且您最初为其提供了默认值,则[nzData]="tableData$ | async"
将起作用。
在这里,异步只意味着它将更新并返回给nzData,即可观察对象内部的值。如果没有异步,您将传递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)));
https://stackoverflow.com/questions/69025230
复制相似问题