Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
Angular - Async pipe in ngIf and text binding will not display first emitted value

I have very simple component template:

<button (click)="emitNextValue()">Next Value</button>

<p *ngIf="text$ | async">
  {{ text$ | async }}
</p>

and component code:

export class AppComponent {
  count = 1;
  text$ = new Subject<string>();

  emitNextValue(): void {
    this.text$.next(`Value ${this.count}`);
    this.count += 1;
  }
}

As you can see, the <p> in the template should be displayed only if text$ Subjects emit value. Clicking the "Next Value" button will call next on the subject and thus should show the paragraph.

The problem is that <p> is displayed only on the second click, which I think is incorrect.

Any idea what I am missing, or this is actual bug?

Demo is available on stackblitz.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This is called “late subscriber” problem. This problem occurs when incoming Rx values arrive before the subscription has happened.

If you remove *ngIf directive from html it will work as expected.

<p>
  {{ text$ | async }}
</p>

For More Info

about 4 years ago · Juan Pablo Isaza Denunciar

0

the subscription happened late. in common case you could make your subscription replay the last event by shareReplay(1) operator or by using ReplaySubject(1) subject. But in your particular case there is a better solution for that

<p *ngIf="text$ | async as text">
  {{ text }}
</p>

ngIf allows to "store" the result of expression which is very usefull for exactly such cases

about 4 years ago · Juan Pablo Isaza Denunciar

0

The fact is that the <p> element is created only after the first event is notified.

So, after the first event is notified you have the element in the Dom and therefore also the {{ text$ | async }}. Then, when a second click occurs, a second event is notified and the binding is triggered.

If you substitute Subject with new ReplaySubject<any>(1) you will have the text displayed also after the first click.

The reason is that the ReplaySubject will replay the last value it emitted as soon as it is subscribed. So, even if the text binding occurs after the first event is notified, the value notified is replayed by the subject and therefore shown in the <p> element.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda