How do I use async pipe instead of using subscribe?

integral100x

I would like to use the async pipe "| async" instead of subscribing. This is what my subscription code currently looks like:

ngOnInit(): void {
  this.activatedRoute.url
    .pipe(takeUntil(this.unsubscribe$))
      .subscribe(segments => {
        this.quizName = segments[1].toString();
      });
}

and in my template I have: <mat-card *ngIf="quiz.quizId === quizName">

Emilien

Let's try this :

quizName$: Observable<any>;

ngOnInit(): void {
  this.quizName$ = this.activatedRoute.url
    .pipe(
      takeUntil(this.unsubscribe$),
      map(segments => segments[1].toString()); // Not sure about this part
    );
}
<mat-card *ngIf="(quizName$ | async) === quiz.quizId">

Be careful, everytime you will use async pipe in your template, it will make a subscribe.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is it really better to use async-pipe instead of subscribe() in this particular situation?

Using async instead of .subscribe

Using Angular Async Pipe to Subscribe to Observable

how can i use async pipe when I'm using graphql queries?

Passing a variable using async/await instead of a subscribe

How do I make a Python async named pipe server using curio?

Do I really need to use the pipe construction instead of `of`?

How do I use subset in a ggplot pipe?

NgrxStore and Angular - Use the async pipe massively or subscribe just once in the constructor

RamdaJS - How can I use pipe with sync and async functions?

The Async pipe doesn't receive a value until I subscribe to the observable

Async pipe and subscribe on the same Observable

How to use async await with subscribe angular

Angular RxJS: Do I need to do .takeUntil(this.destroy$) if I'm using async pipe?

How do I use async and await in this code?

How do I use async/await in JavaScript?

How do I subscribe to channel updates using Telegram API?

How do I subscribe to an event using task expressions?

Why should I use select with pipe in Angular subscribe?

how do I use current node instead of this?

How do I subscribe to a bug?

How do I use the pipe command to display attributes in a file?

How do I use a serial port on Linux like a pipe or netcat?

ngrx: how to combine observables for use in async pipe

how to use angular pipe and subscribe correctly in request call

How do I filter and then calculate the mean using the pipe operator?

How do I write stderr to a file while using "tee" with a pipe?

Angular async pipe doesn't subscribe

rxjs observable doesn't subscribe with async pipe