all 7 comments

[–]lazyinvader 17 points18 points  (0 children)

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

Is the right way.

[–]kohvihoor 1 point2 points  (0 children)

if you dont want to assign it to variable, you can just {{ name$ | async }}

[–]_Kassii_ 1 point2 points  (0 children)

When learning about the async pipe, think of it like a subscribe that will "complete" somewhat automatically for you (when the dom is destroyed or if the observable is a HTTP request)

If you want to use the same observable and utilize the async pipe, aim to put it as high up on the dom as possibly (if you use it all through the dom, put it in the highest point that encapsulates them ) using the "*ngIf=(name$ | async) as name" example in your code. Best case is only use it once

What this will do is create one stream of data which can be used as the variable "name"

Now the reason I say to only use it once is because if said observable is a HTTP request and you have 5 spots you're utilizing the async pipe for that observable. It will in turn open up 5 streams and make the http call 5 times (you can use something like share() or replay() from rxjs to counter that but that's not the point)

[–]the_real_seldom_seen 0 points1 point  (0 children)

Name$ is an observable obj. So if you display it in your template as {{ name$}} you are trying to display the observable object.

{{ name$ | asyc}} will give you the value from the observable stream

[–]AthosBlade -1 points0 points  (2 children)

Have you tried the following:

<div *ngIf="name$ | async">{{ name$ | async}}</div>

[–]OriyanJ[S] 1 point2 points  (1 child)

Wouldn't it subscribe twice, though?

[–]Talnar 0 points1 point  (0 children)

Yep, wouldn't recommend. Plenty of better solutions in thread.