4

I have the following observable on and Angular component:

count$: Observable<number>;

this.count$ = this.getCount();

By using the following I get the value 0 (Zero);

this.count$.subscribe(x => console.log(x));

On the template I have:

<a routerLink="/dash" *ngIf="(count$ | async)">
  <ng-container *ngIf="count$ | async as count">
    Count: {{count}}
  </ng-container>
</a> 

If count$ is 1 I get Count: 1 but if count$ is 0 the content Count: 0 is not even rendered.

Any idea why?

Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73
Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

4 Answers4

13

In case anyone is looking for a workaround, this so stupid but it works:

   <ng-container *ngIf="{ len: count$ | async } as ctx">
      <ng-container *ngIf="ctx.len !== null"        
          {{ ctx.len }} 
       </ng-container>
   </ng-container>

Explanation:

  1. Capture count as len inside of an object called ctx
  2. Because *ngIf is now looking at an object, instead of the value, it will evaluate as truthy and render the ng-container
  3. Now we check whether we got an actual value or not with ctx.len !== null
  4. We can access the value using ctx.len
Patrick
  • 999
  • 1
  • 9
  • 21
6

As discussed in this issue, this is the expected behavior. NgIf coerces the given expression into a boolean value. If the expression evaluates into a falsy value (values that are translated into false) then the content wont be rendered.

The following are all the values that javascript currently translates to false:

if (false)
if (null)
if (undefined)
if (0)
if (NaN)
if ('')
if ("")
if (``)
Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73
3

You're using an ngIf, means you are doing a conditional display of your value. When the value is 0, your condition evaluates to false and thus won't be displayed.

seawave_23
  • 1,169
  • 2
  • 12
  • 23
0

In these cases:

    if (false)
    if (null)
    if (undefined)
    if (0)
    if (NaN)
    if ('')
    if ("")
    if (``)

These are all falsy as stated above, here is my suggestion: By using a pipe(map()) to convert it to string, that way '0' is not falsy and it can still be used with number pipe, etc.

Hope this helps some else.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 24 '21 at 04:37