Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 4 -using else conditions with ng-template in p-datatable. currently have 2 ngif inside the template, want to avoid using two ngif

Tags:

angular

In a component I have a source code similar to this:

<ng-template let-col let-row="rowData" let-rowIndex="rowIndex" pTemplate="body">
  <span *ngIf='row.job_id'>
    {{row.job_id}}
  </span>
  <span *ngIf='!row.job_id'>
    Job ID Not available
  </span>
</ng-template>

I want to use else condition with it inside a single ng-template. how can I achieve it?

like image 333
rakesh Avatar asked Nov 22 '25 12:11

rakesh


1 Answers

If you only need to change the text inside the span you can simple use :

{{ row.job_id ? row.job_id : 'Job ID Not available' }} 

like this you avoid *ngIf

If you really want to use if/else this seem to work :

<ng-template let-col let-row="rowData" let-rowIndex="rowIndex" pTemplate="body">
    <span *ngIf='row.job_id; else notAvailable'>
       {{row.job_id}}
    </span>
    <ng-template #notAvailable>
      <span>
        Job ID Not available
      </span>
    <ng-template>
</ng-template>
like image 55
xrobert35 Avatar answered Nov 25 '25 02:11

xrobert35