68

Hi I have a observable user$ with a lot of properties (name, title, address...)

component{
  user$:Observerable<User>;
  constructor(private userService:UserService){
    this.user$ = this.userService.someMethodReturningObservable$()
  }
}

Is there a way to use the async pipe in the html template to subscribe to it and bind it to a local variable like this

<div #user="user$ | async">
  <h3> {{user.name}}
</div>

I know can can subscribe to it in the constructor and then unsubscribe in OnLeave/OnDestroy but I was just curious if I could use the async pipe.

Cheers

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
Han Che
  • 8,239
  • 19
  • 70
  • 116
  • As I know async pipe only works with *ngFor ? in that you can do this `*ngFor="let u of (user = (user$ | async))"` but seems fishy. need a bit practical. – Babar Hussain Apr 10 '17 at 14:38
  • not async can work on a single observable as well, see ngrx/store :) – Han Che Apr 10 '17 at 14:46

4 Answers4

153

# is template reference variable. It defers to DOM element and cannot be used like that.

Local variables aren't implemented in Angular as of now, this closed issue can be monitored for the references to related issues.

Since Angular 4 the syntax of ngIf and ngFor directives was updated to allow local variables. See ngIf reference for details. So it is possible to do

<div *ngIf="user$ | async; let user">
  <h3> {{user.name}} </h3>
</div>

This will create div wrapper element and will provide cloaking behaviour to it, so there's no need for ?. 'Elvis' operator.

If no extra markup is desirable, it can be changed to

<ng-container *ngIf="user$ | async; let user">...</ng-container>

If cloaking behaviour is not desirable, the expression can be changed to truthy placeholder value.

A placeholder can be empty object for object value,

<div *ngIf="(user$ | async) || {}; let user">
  <h3> {{user?.name}} </h3>
</div>

Or a space for primitive value,

<div *ngIf="(primitive$ | async) || ' '; let primitive">
  <h3> {{primitive}} </h3>
</div>
Mathias
  • 1,819
  • 4
  • 22
  • 34
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • 1
    This is working for me, however I'm getting a warning saying `'of' expected`. I think this is a tslint warning, but having a hard time finding any info for it. – Blexy Jul 11 '17 at 18:53
  • @Blexy It's hard to say what's wrong here, it looks like a part of some message. – Estus Flask Jul 11 '17 at 19:12
  • 1
    Yeah I'm getting the same error with the `'of' expected`, I was trying to reform it so it would make sense to both the linter and Angular, but that turns out to be a bit harder then expected. Feels like some automagic with the `async` pipe. – Bjorn 'Bjeaurn' S Aug 02 '17 at 09:02
  • @BjornSchijff did you identify a workaround for `'of' expected`? – Blexy Sep 29 '17 at 01:10
  • @Blexy You didn't mention that this is [IDE warning](https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000491110-Angular-4-HTML-template-of-expected-error), in fact. TSLint doesn't care about templates. – Estus Flask Sep 29 '17 at 02:39
  • @Blexy see the answer of Wooli Design, this is what worked for me. In regards to your IDE warning; if you use `ng cli`, this does care about templates and will throw errors if incompatibilities have been found. – Bjorn 'Bjeaurn' S Nov 16 '17 at 10:00
  • @BjornSchijff To my knowledge, `as` and `let` are interchangeable, the latter is closer to how the directive is desugared without `*`, both are supported by Angular and Angular CLI. Do you have information that contradicts this? – Estus Flask Nov 16 '17 at 12:26
42

@Bjorn Schijff and @estus

Instead of:

<div *ngIf="(user$ | async) || {}; let user">

Do:

<div *ngIf="(user | async) as user">
Wooli Design
  • 421
  • 4
  • 3
8

Use following syntax:

<div *ngIf="(user | async) as user"> 

Note: The addition of “as user” at the end of the expression.

What this will do is wait until user$ | async has evaluated, and bind the result to the value of user (non-dollar-suffixed).

s sharif
  • 724
  • 1
  • 10
  • 19
1

For *ngFor it's possible with added *ngIf directive on parent element:

    <ul *ngIf="users$ | async as users">
      <li *ngFor="let user of users">{{user.name}}</li>
      <li *ngIf="!users.length">No users</li>
    </ul>

If there's already a structural directive on the parent element, then introduction of an intermediate element like <ng-container> may be necessary.

Daniel Kucal
  • 8,684
  • 6
  • 39
  • 64