Search code examples
angularconditional-statementsinterpolationbehaviorsubject

Conditional interpolation with subject


I have an Subject which provides me with number. In my template, I would like to render this number in braces, if it is higher than 0.

Theoretically something like this (this does not work though):

{{ ($number | async) > 0 ? '('+ ($number| async) +')' : '' }}

How would I render an subject, based on a condition of its own value?


Solution

  • use ngIf conditions and assign the observable result to variable

    <div *ngIf="$number | async as num"> 
       <div *ngIf="num> 0"> '(' {{num}} ')' </div>
    </div>