Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular: Use Pipe Date with Format

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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;
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading