角材料表排序-在* ngIf条件中时,数据不排序 [英] Angular Material Table Sorting - Data Doesn't sort when inside a *ngIf condition
问题描述
我有一个角材料表.当我用<div *ngIf="true">
包围表格的html时,表格将呈现,但单击标题列时数据不再排序.
I have a Angular Material Table. When I surround html for the table with a <div *ngIf="true">
the table renders but the data no longer sorts when clicking on the header column.
以以下示例为例: https://material.angular.io/components/表格/概述#sorting
并对其进行修改,只需添加<div *ngIf="true"> ... </div>
即可演示此行为.示例如下: https://stackblitz.com/edit/angular-quzvjv
And modifying it, just by adding the <div *ngIf="true"> ... </div>
demonstrates this behavior. Example is at: https://stackblitz.com/edit/angular-quzvjv
推荐答案
在控制台上登录this.sort
的ngOnInit
在初始化阶段NgIf未处理模板时,Angular没有捕获到MatSort
组件.
Angular didn't catch MatSort
component as on init phase NgIf didn't process the template.
将ngOnInit
更改为ngAfterViewInit
,它将按预期工作.
Change ngOnInit
to ngAfterViewInit
and it will work as expected.
这篇关于角材料表排序-在* ngIf条件中时,数据不排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!