angular - *ngIf on tr 未更新
问题描述
我有一个带有 ngIf 语句的 tr,该语句取决于函数结果 - 该函数由 click 事件(toggle(data.code))执行。查看我的代码:*TableData 是记录的集合
<tbody *ngFor="let data of TableData" (click)="toggle(data.code)">
<tr *ngIf="isExpanded(data.code)">
<td *ngfor="let column of columns">{{column.name}}</td>
</tr>
这是组件中的功能:
codesArray: number[] = [];
isExpanded(code)
{
return (code%10 == 0) || this.codesArray.includes(code); // The second part of the condition changes and should effect the tr
}
toggle(code)
{
if (this.codesArray.includes(code))
{
this.codesArray.splice(this.codesArray.indexOf(code), 1);
}
else //This part should happen first and change the tr state
{
this.codesArray.push(code); //This actually happens but the tr not appear
}
}
是否需要某种变化检测?
解决方案
不要在 body 上使用 ngFor。您将获得多个表格主体。尝试使用 ng-content。
<tbody>
<ng-container *ngFor="let data of TableData">
<tr (click)="toggle(data.code)">
<ng-container *ngIf="isExpanded(data.code)">
<td *ngfor="let column of columns">{{column.name}}</td>
</ng-container>
</tr>
</ng-container>
推荐阅读
- c# - 按嵌套列表c#上的属性排序
- python - 与 NaN 相等的元素比较
- python - Convert List to String and back to List Python 3
- python - 具有无量纲坐标的 Xarray plot.line
- ios - 为了消除 Xcode 中的错误,我需要在 Xcode 或我的开发人员配置文件中进行哪些更改?
- database - 根据一系列逻辑语句识别重复记录并识别唯一记录
- tfs - TFS 构建任务以自动将 ID 添加到我们的 HTML 标记
- azure - 通过 Azure 设置 SSL 证书后运行本地主机的问题
- opencart - Opencart 3路线不起作用,但应该
- c# - C#如何在某个标题上打开pdf