0

I have a condition like this.

<div *ngIf="data$  | async as data; else loading">

I wanted to add additional condition into above statement, how am I able to do that?

<div *ngIf="data$ && isLoading | async as data; else loading" > error

<div *ngIf="data$ | async as data && isLoading ; else loading" > error

also for the | operator what is the name called I tried to google it but no result found. Or operator, signle line operator, line operator?

2
  • what about using parens around the observable...*ngIf="(data$ | async as data) && !isLoading ; else loading"? And the | is called pipe. Commented Mar 9, 2021 at 16:03
  • I tried it. The error shows Expected identifier or keyword expression expected after data and between async and as it says ) expected Commented Mar 9, 2021 at 16:06

1 Answer 1

1

I want to suggest handling only one variable on if statements, hence you can handle only data$ as your condition to evaluate and handle the other conditions on your observable/promise.

Example:

<!-- on my template this looks cleaner -->
<div *ngIf="myObservable$  | async as data; else loading">
myObservable$ = of({}).pipe(
  map((response) => response && this.isLoading) // <- here I evaluate my conditions
)

However, if you want to evaluate your conditions on the template then you can:

<div *ngIf="(data$ | async) && isLoading; else loading">
Sign up to request clarification or add additional context in comments.

Comments

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.