Muthukumar Marichamy
Muthukumar Marichamy

Reputation: 1214

Ng If does not work in observable variable in Angular 6

I have integrated the Angular6 material data grid with server side pagination as per this link https://blog.angular-university.io/angular-material-data-table/

Here, I want to display the "No Data Found", if the data set is empty from the response. I could take the totalCount which is mapped Observable and I can print the view as below. But, ngIf does not work.

  private totalCountSubject = new BehaviorSubject([]);
  public totalCount$ = this.totalCountSubject.asObservable();
  this.totalCountSubject.next([body.data.count]);

  // View
  {{dataSource.totalCount$}} <!-- It displayes 0 or count of the row -->

   <!-- It does not work. --->
   <span *ngIf="(dataSource.totalCount$ | async) === 0 ">
     NO DATA FOUND!!!!
   </span>

Any idea why ngIf does not work in this case.

Upvotes: 1

Views: 233

Answers (1)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41407

handle the condition with nested ngIf s

<ng-container *ngIf="dataSource.totalCount$ | async as totalCount">  
   <span *ngIf="totalCount == 0 ">
     NO DATA FOUND!!!!
   </span>

<ng-container>

Upvotes: 1

Related Questions