angular - IE11 不在动态表中渲染 DOM 元素
问题描述
使用 Angular 过滤表格数据时,我在 IE11 上遇到了奇怪的行为。
<ng-container *ngFor="let item of items; trackBy: trackByFn">
<tr>...</tr>
<tr *ngIf="showDetails(item)">...</tr>
</ng-container>
items
发生的情况是,当 IE11 中发生更改(即搜索或过滤数据时)时,某些行未正确呈现:
正如您在上图中所见,<tr>
元素在那里,其内容也在那里。绿色行渲染得很好,但红色行显然不是。只要我悬停空行,它的内容就会再次出现。
该问题可能是由ng-container
每两行的包装引起的,IE 无法正确处理此问题。有什么想法可以解决这个问题吗?
解决方案
不幸的是,删除元素中的空格<td>
并不能解决问题。但是,我确实设法通过*ngIf
仅为 Internet Explorer 创建一个自定义指令来使其工作,这会强制浏览器重新呈现表格:
对于那些遇到同样问题的人,这里是代码:
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
/**
* Add the template content to the DOM when the condition is true for IE browsers only.
*/
@Directive({
selector: '[appIfIE]'
})
export class IfIeDirective {
private hasView = false;
private isIE = navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > -1;
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef
) {}
@Input() set appIfIE(condition: boolean) {
if (!this.isIE && !this.hasView) {
this.createView();
} else if (this.isIE && condition && !this.hasView) {
this.createView();
} else if (this.isIE && !condition && this.hasView) {
this.clearView();
}
}
createView() {
this.viewContainer.createEmbeddedView(this.templateRef);
this.hasView = true;
}
clearView() {
this.viewContainer.clear();
this.hasView = false;
}
}
...并在您认为合适的地方应用指令:
<tbody *appIfIE="!loading">
干杯!
推荐阅读
- oracle - AWS Oracle RDS DATA_PUMP_DIR 清理
- nvidia - 正确创建 Optix 7.1 TLAS 实例加速结构
- asp.net-mvc - 如何使用 ASP.NET MVC DropDownListFor() 发布选择列表值?
- c# - 无法在 .NetFramework 的类库中创建剃刀视图
- mongodb - 在没有allowDiskUse的情况下删除mongodb数据库中的重复项
- python - Google drive api python - 如何获取下载文档的可共享链接
- linux - file_put_contents() 无法打开流:CentOS 上的权限被拒绝
- python - 如何使用 matplotlib.animation (python)逐点为脱节的线设置动画?
- php - 致命错误:未捕获的 ArgumentCountError:mysqli_connect_errno() 需要 0 个参数,1 个给定
- safari - Safari 14 中的 CSS 转换功能