首页 文章

* AngFor 5中<tr>元素的ngFor和* ngIf

提问于
浏览
0

在我的Angular 5应用程序中,我正在循环创建 tr 元素:

<tr *ngFor="let element of elements">

但我只想在符合某些条件的情况下显示该行 . 我知道我不能一起使用 *ngFor*ngIf 但是我没有看到如何解决这个问题 .

我看到一篇帖子说要在一个带有 [ngIf] 结构的tr中添加一个模板,但这似乎不再是有效的语法 . 如果我这样尝试:

<tr *ngFor="let element of elements">
    <template [ngIf]="element.checked || showUnchecked">

然后我得到运行时错误 .

AppComponent.html:14 ERROR错误:StaticInjectorError(AppModule)[NgIf - > TemplateRef]:StaticInjectorError(Platform:core)[NgIf - > TemplateRef]:NullInjectorError:没有TemplateRef的提供者!

2 回答

  • -1

    创建一个返回已过滤的属性元素的getter . 就像是

    get filteredElements() {
        if( this.showUnchecked ) {
            return this.elements;
        } else {
            return this.elements.filter( () => { return this.checked; } );
        }
    }
    
  • 0

    ng-container将工作而不是模板

    <table><tr *ngFor="let element of elements">
    <ng-container *ngIf="element.checked || showUnchecked">
      <td></td>
      <td></td>
    </ng-container>
    </tr>
    <table>
    

相关问题