1

I'm relatively new to reactive programming and I'm trying to create an Angular service which can display notifications to the user. So far this is what I have:

https://stackblitz.com/edit/angular-rxjs-notifications?file=app%2Fapp.component.html

But I noticed that when you click the "Queue Notification" button my *ngIf="message$ | async" <div> appears correctly but the inner {{ message$ | async }} text doesn't appear unless you click the button twice in a row without clicking "Clear"

Thanks in advance!

4
  • Can you split this into two separate questions? The second one definitely deserves a thread of its own. Commented Apr 18, 2018 at 19:37
  • @TomaszKula Done. Commented Apr 18, 2018 at 19:49
  • Link to the second question? Commented Apr 18, 2018 at 19:53
  • @TomaszKula stackoverflow.com/questions/49907996/… Commented Apr 18, 2018 at 19:55

1 Answer 1

1

Your first problem can be solved like this.

<div *ngIf="message$ | async as messages" >
  <div>{{ messages }}</div>
</div>

We store the result from the pipe in a local variable called messages. This way you avoid a situation in which the inner async pipe is not called after the outer one emits.

Live demo

Sign up to request clarification or add additional context in comments.

3 Comments

I see that your live demo is working but unfortunately in my actual application I am using a third-party popup component where my template looks like this <dx-popup [visible]="message$ | async as message"> with {{ message }} inside that. Is this as operator something special only to *ngIf?
Yes, the as is only allowed in *ngFor and *ngIf. Wrap your component in <ng-container *ngIf="message$ | async as message"> and pass the message as the input.
Glad I could help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.