88

I need to use async pipe without ngFor. I need to check property of object that is async loaded with observable.

This is what i want, but not working:

 <ion-item *ngIf="user$.anonymouse | async">
     <ion-label>Login</ion-label>
 </ion-item>

//EDIT: I getting this error when i use code above

EXCEPTION: Invalid argument 'true' for pipe 'AsyncPipe' in [!user$.anonymouse | async in SettingsPage@27:22]

Is there any way how to solve this?

I know i can subscribe to this observable in Ctrl a store values into normal variable but i dont want to do that, because of perfomance etc.

Patricio Vargas
  • 5,236
  • 11
  • 49
  • 100
Daniel Suchý
  • 1,822
  • 2
  • 14
  • 19

4 Answers4

75

The error is surprisingly accurate as the *ngIf directive expects true or false and uses the resulting expression to determine whether or not to render the HTML element in the DOM.

EXCEPTION: Invalid argument 'true' for pipe 'AsyncPipe' in [!user$.anonymouse | async in SettingsPage@27:22]

The expression you have is user$.anonymouse which evaluates as truthy, but unfortunately you cannot use the async pipe with this directive. The async pipe "transforms" (also known as "pipes") the input exposing the resulting output within the scope of the *ngFor directive for example.

The pipe expects one of three possible types, defined below (detailed about AsyncPipe):

transform(obj: Observable<any>| Promise<any>| EventEmitter<any>)

Is there any way how to solve this?

Yes, you can either use it as it was designed. For example in an *ngFor directive:

<ion-item *ngFor="(user$ | async)?.anonymouse">
     <ion-label>Login</ion-label>
</ion-item>

Or you could remove the piping altogether as it's not needed for the *ngIf directive:

<ion-item *ngIf="user$.anonymouse">
     <ion-label>Login</ion-label>
</ion-item>
David Pine
  • 23,787
  • 10
  • 79
  • 107
  • 71
    Need to clarify the use of AsyncPipe with NgFor as discussed above. The expression `*ngFor="user$.anonymouse | async"` will pass the `anonymouse` property of `user$` to AsyncPipe, which I believe would be `undefined` because `user$` is an observable, not the output of the observable. In order to run AsyncPipe on `user$` and then access the `anonymouse` property on the result, you would need to use `*ngFor="(user$ | async)?.anonymouse"`. – Sean May 24 '16 at 16:42
  • 4
    I think the comment by @Sean should be an answer and marked as the accepted answer. The accepted answer gives a nice explanation; however, as pointed out by Sean, `(user$ | async)?.anonymouse` is what the OP needs to do (and what worked for me! Thanks!) – Kabb5 Aug 03 '17 at 15:17
71

As stated in the comments by @Sean, your *ngIf statement should be based on the resulted object property anonymouse of the user$ object that is returned. As such:

<ion-item *ngIf="(user$ | async)?.anonymouse">
     <ion-label>Login</ion-label>
</ion-item>

This worked for me and here is an example of how to use the results from the pipe below:

Component

 message$: Observable<{message: string}>;

  private messages = [
    {message: 'You are my hero!'},
    {message: 'You are the best hero!'},
    {message: 'Will you be my hero?'}
  ];

  constructor() { this.resend(); }

  resend() {
    this.message$ = Observable.interval(500)
      .map(i => this.messages[i])
      .take(this.messages.length);
  }

View

<h2>Async Hero Message and AsyncPipe</h2>
<p>Message: {{ (message$ | async)?.message }}</p>
<button (click)="resend()">Resend</button>`

A working example can be found here.

gusgonnet
  • 107
  • 2
  • 9
Abdullah Rasheed
  • 3,562
  • 4
  • 33
  • 51
43
<!-- This is needed to wait for async pipe to resolve -->
<div *ngIf="user$ | async as user"> 

   <!-- Only on resolve of async pipe -->
   <ion-item *ngIf="user.whateverPropertyYouWantToCheck">
        <ion-label>Login</ion-label>
    </ion-item>
</div>

Please note that I switched from user$ to user, you can use the same variable name if you want to, but this makes it more clear that the value is no longer an async pipe.

TimNode
  • 733
  • 6
  • 7
0

isAnonymouse$ = user$.pipe(map(user => user?.anonymouse));

<ion-item *ngIf="user$.anonymouse | async">