angularrandommat-card

Display Angular 6 mat-card sourced by Firebase Data in random order on Page Load/Refresh


I display data from firebase. I want to show my cards in random order when I change the value of a form or refresh the page. It is possible?

Here's my Component Template:

<ng-container *ngFor="let geoToDisplay  of geosToDisplay | async">
  <ng-container *ngIf="toppings2.value.includes(geoToDisplay.esSos)">
    <ng-container *ngIf="toppings.value.includes(geoToDisplay.typeSos)">
      <div class="col">
        <mat-card>BLABLA
        </mat-card>
      </div>
    </ng-container>
  </ng-container>
</ng-container>

Solution

  • You can use

    setInterval(() => {
        this.randomCard = this.geosToDisplay[Math.floor(Math.random() * this.geosToDisplay.length)]; // this'll get the random value depending on your array length
      }, 30000);
    

    and bind it as,

     <mat-card >{{randomCard}}</mat-card>