Angular: Use Pipe Date with Format

Advertisements

I have an elevator component. Whenever the elevator reaches the first floor, store the date and display it in dd/MM/yy format.

if floor === 1 show Date
else
return(empty)

The function I was trying to run is GetDate

My stackblitz : https://stackblitz.com/edit/angular-ivy-zbrapb?file=src%2Fapp%2Fapp.component.ts

>Solution :

You try to get a number out of an Observable. That will not work. I’ve added a new var and a tap for a side effect. That will work. There are other ways to achieve this; create a Subject that will receive a next(true/false) based on the floor number and use ngIF to display the date.

export class AppComponent implements OnInit {
  floor!: Observable<Number>;
  floorNumber: number = -1;
  myDate = new Date();

  ngOnInit(): void {
    this.floor = this.getNumbersInfinite();
  }

  getNumbersInfinite() {
    return interval(1000)
    .pipe(
      map(() => Math.floor(Math.random() * 3) + 1),
      tap(res =>this.floorNumber=res));
  }

  GetDate() {
    if (Number(this.floorNumber) === 1) {
      return true;
    }
    return false;
  }
}

Leave a Reply Cancel reply