3

I develop an Angular app. I want to display some text when my app is not in production, otherwise nothing. I have the following code, where environment is an observable:

<span *ngIf="(environment | async) !== 'production'">bla bla text...

The problem is that it shows the content as long as the observable is not resolved.

I think this resolves to :

undefined !== 'somestring'

and therefore the condition is validated and the text displayed. This is not what I want, I want no display, no evaluation of the expression before resolution of the observable.

What syntax should I use to prevent the flash of undesired content ?

thanks for any help

Olivvv
  • 1,140
  • 1
  • 13
  • 37

2 Answers2

5

Subscribe once and for all in your component, and test the actual emitted value in your template:

<span *ngIf="env && env !== 'production'">

Or subscribe once in your template, and store the result in a variable:

<ng-container *ngIf="environment | async as env">
  <span *ngIf="env !== 'production'">
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • thanks for your answer. Not using observable in template is indeed a solution, but wanted a way to use them in there. Your second proposal is a bit verbose imo, and not convenient to generalize. – Olivvv Dec 18 '19 at 18:02
  • It is the recommended way to use async: you subscribe once, and only once, and assign the result to a variable that you can use as many times as you want without resubscribing again and again. Remember that every time you use async in a template, you subscribe to the observable. If it's cold, you thus trigger the pipeline again. If it's an HTTP observable, you send a new HTTP request every time. – JB Nizet Dec 18 '19 at 18:07
  • So it is not good to use observables directly in a template ? Then the async pipe is a little bit misleading toward the wrong pattern (we had that kind of cases already in angularjs) – Olivvv Dec 18 '19 at 18:12
  • It is absolutely fine, but you must understand how it works, what it implies, and how best to write your code to avoid nasty situations. – JB Nizet Dec 18 '19 at 18:13
  • Sure, this is true for everything. Until now I thought that async pipe was totally harmless, and that it was better to shrink the component size and push stuff into the template. I ll know better now – Olivvv Dec 18 '19 at 18:16
2

You could test for the undefined, null and 'production' with Array.indexOf:

<span *ngIf="[undefined, null, 'production'].indexOf(environment | async) < 0">
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146