https://stackblitz.com/edit/angular-qq6zi1?file=src/app/app.component.ts
columns: Array<{ key: 'string', title: string }> 用于动态设置 table 所展示的列<thead [nzSingleSort]="true" (nzSortChange)="onSortChange($event)">*ngFor 生成一系列的 <th nzShowSort [nzSortKey]="col.key">{{ col.title }}</th>th 进行排序操作,发现只触发一次 onSortChangecolumns 数组,如删除一项th 进行排序操作,发现 onSortChange 会触发两次onSortChange 会触发三次nzSortChange 不应当反复触发
nzSortChange 反复触发
| Environment | Info |
|---|---|
| ng-zorro-antd | 7.5.1 |
| Browser | Chrome/74.0.3729.169 |
https://stackblitz.com/edit/angular-qq6zi1?file=src/app/app.component.ts
columns: Array<{ key: 'string', title: string }> to dynamically set the columns displayed by table<thead [nzSingleSort]="true" (nzSortChange)="onSortChange($event)"><th nzShowSort [nzSortKey]="col.key">{{ col.title }}</th> by*ngFor`.th sorting operations and find that only one time is called onSortChange.columns array, such as deleting an itemth to sort and find that onSortChange will fire twice.onSortChange will trigger three times.[Video] (https://s3.ssl.qhres.com/static/18b135736269b69b.mp4)
nzSortChange should not be triggered repeatedly
nzSortChange is triggered repeatedly
| Environment | Info |
|---|---|
| ng-zorro-antd | 7.5.1 |
| Browser | Chrome/74.0.3729.169 |
@vthinkxie 大概定位了 bug 的原因。主要在这里:components/table/nz-thead.component.ts#L66
flatMap(() =>
merge<{ key: string; value: string }>(...this.listOfNzThComponent.map(th => th.nzSortChangeWithKey))
),
写了段原理类似的代码 ——
import { merge, Subject } from "rxjs";
import { startWith, flatMap } from "rxjs/operators";
const subject = new Subject();
const array = [new Subject(), new Subject(), new Subject()];
subject
.pipe(
startWith(true),
flatMap(() => {
console.log(array.length);
return merge(...array);
})
)
.subscribe(console.log);
array[0].next("hello");
array[1].next("world");
array.pop().next("!");
subject.next(123);
array[0].next("hello");
array[1].next("world");
执行效果如下:

上面的例子 flatMap 换成 switchMap 就好了。我提个 PR ?
import { merge, Subject } from "rxjs";
import { startWith, switchMap } from "rxjs/operators";
const subject = new Subject();
const array = [new Subject(), new Subject(), new Subject()];
subject
.pipe(
startWith(true),
switchMap(() => {
console.log(array.length);
return merge(...array);
})
)
.subscribe(console.log);
array[0].next("hello");
array[1].next("world");
array.pop().next("!");
subject.next(123);
array[0].next("hello");
array[1].next("world");

@AngusFu 非常感谢
@vthinkxie 🙂 刚刚搜了下,貌似整个 repo 中还有另外三个地方使用了 flatMap,其出现的场景大体和本 issue 类似((<ContentChildren>something).changes )。table 中有一个,submenu 和 select 中也各有一个。建议可以确认排查下。
https://github.com/NG-ZORRO/ng-zorro-antd/search?q=flatMap&unscoped_q=flatMap
Most helpful comment
@vthinkxie 🙂 刚刚搜了下,貌似整个 repo 中还有另外三个地方使用了
flatMap,其出现的场景大体和本 issue 类似((<ContentChildren>something).changes)。table 中有一个,submenu 和 select 中也各有一个。建议可以确认排查下。https://github.com/NG-ZORRO/ng-zorro-antd/search?q=flatMap&unscoped_q=flatMap