2

I have 2 functions that checks a specific condition, I would both like both of them to be true. How would you use *ngIf in this case? Currently if I set one of them it works, but I need both of them.

HTML

<p *ngIf="(isFree$ | async) && (order$ | async)">Free Plan</p>

TS

public order(data) {
    const order = data.externalOrderId;
    if (order.substring(0, 4) === 'asdad') {
      return true;
    } else {
      return false;
    }
  }


 public isFree$ = this.planType$.pipe(
    map((data) => {
      return (data.plan === 'Free');
    }
));

2 Answers2

0
  • Create a new dumb component that has (2) Inputs: [isFree], [order]

  • In your new component use an *ngIf statement to show the <p> if both Inputs are available.

Flignats
  • 1,234
  • 10
  • 21
  • What do you mean? –  Jan 09 '20 at 18:08
  • The parent component should contain the observables and provide them as `input`s to a dumb component that has your `p`. Only show the `p` element if both inputs are defined – Flignats Jan 09 '20 at 19:23
0

You can use rxjs forkJoin

It will wait until both the observables streams return the response. It returns single observable array of responses to which you can subscribe or use async pipe to resolve

HTML

<p *ngIf="resp$ | async">Free Plan</p>

TS

public isFree$ = this.planType$.pipe(
    map((data) => {
      return (data.plan === 'Free');
    }
));

public isOrder$ = of({resp:[]}) // stream 2

public resp$ = forkJoin(this.isFree$, this.isOrder$)

Fork join example with ngIf and async

Amit Chigadani
  • 28,482
  • 13
  • 80
  • 98