0

I'm new to Angular, and I need help with displaying a message when there is no data to show.

Here's my component.html part:

<app-header [isHidden]="isHiddenProgressBar"></app-header>
<div class="change">
  <button (click)="changeState()" *ngIf="showButton" color="primary" mat-raised-button>Change state</button>
</div>
<div class="list">
  <app-list *ngIf="visible" [changeObjects]="data"></app-list>
</div>

And I have a boolean in the .ts file as follows:

  this.data = this.objects;
      this.visible = this.objects != null && this.objects.length > 0;
      this.showButton = true;
    }

I would like the component to display the list if there's data to show, otherwise I would like to display a message like 'The list is empty.'

Thank you for your help!

1 Answer 1

1

You can use ngIf directive to render ng-template when the argument is not truthy. More you can read here.

Example for your code:

<app-list *ngIf="visible; else #notVisible" [changeObjects]="data"></app-list>
<ng-template #notVisible>The list is empty.</ng-template>
Sign up to request clarification or add additional context in comments.

2 Comments

Great, thank you! One more question: is there a way to change the position of the message, like in the middle of the screen? Currently, it's in the top left corner.
You can put the ng-template anywhere in the component template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.