Home > Enterprise >  condition using ngTemplateOutlet && ng-template ignored
condition using ngTemplateOutlet && ng-template ignored

Time:12-24

I have condition using to show/not show my data. there are 2 problems with the code:

  1. if the condition [ngIf]="df == 'srCreationDate'" false, it's noe effect the data (like there is no condition)

  2. I have to iterate the loop just one time, but when I am adding | slice:0:1 pipe nothing appears

    <ng-template #titleCreationDate>
    <div mat-sort-header="srCreationDate"></div>
    </ng-template>
    
    <ng-container *ngFor="let df of dynamicFields ">
    <ng-container [ngIf]="df == 'srCreationDate'" *ngTemplateOutlet="titleCreationDate"></ng-container></ng-container>
    

CodePudding user response:

Here is the correct template code that you should be using:

  <ng-template #titleCreationDate>
    <div mat-sort-header="srCreationDate"></div>
  </ng-template>

  <ng-container *ngFor="let df of dynamicFields ">
    <ng-container *ngIf="df === 'srCreationDate'">
      <ng-container *ngTemplateOutlet="titleCreationDate">
      </ng-container>
    </ng-container>
  </ng-container>

StackBlitz demo.

CodePudding user response:

You have to use *ngIf="..." like that:

<ng-container *ngFor="let df of dynamicFields  | slice:0:1"><ng-container *ngIf="df === 'srCreationDate'" *ngTemplateOutlet="titleCreationDate">

And it is better to use "===" instead of "==".

  • Related