Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle `ngIf` with async pipe values?

I am trying to implement the ngIf with using async pipe. but not works. any one help me here?

here is my html:

<div class="response-container">
    xx {{response | async | json }}
    <div class="failed-error" *ngIf="(response.responseFail | async )">
        {{response.message}}
    </div>
    <div class="success-msg" *ngIf="(response.responseSucc | async) === 'true' ">
        {{response.message}}
    </div>
</div>

in the above xx {{response | async | json }} it prints as:

xx { "responseFail": false, "responseSucc": true, "message": "success" }

But why this is not works with *ngIf condition?

like image 438
user2024080 Avatar asked Jan 01 '23 19:01

user2024080


2 Answers

The response is a data source while response.responseFail is not one. Try this:

*ngIf="(response | async )?.responseFail"

*ngIf="(response | async)?.responseSucc  === 'true' "
like image 150
Ethan Vu Avatar answered Jan 08 '23 14:01

Ethan Vu


you have to access the object after the async pipe =>

*ngIf="(response | async )?.responseFail"

ex =>

https://stackblitz.com/edit/angular-tvmqzm

edit : ethan got here first.

like image 37
Crocsx Avatar answered Jan 08 '23 15:01

Crocsx