【问题标题】:Conditional interpolation with subject带主体的条件插值
【发布时间】:2019-04-17 06:05:29
【问题描述】:

我有一个Subject,它为我提供了number。在我的模板中,如果它大于 0,我想在大括号中呈现这个数字。

理论上是这样的(虽然这不起作用):

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

我将如何根据其自身价值的条件渲染一个主题?

【问题讨论】:

    标签: angular conditional interpolation behaviorsubject


    【解决方案1】:

    使用ngIf 条件并将可观察结果分配给变量

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

    【讨论】:

    • 正是我需要的。在我的例子中,我使用span 将数字添加到内部 html 文本中。
    【解决方案2】:

    尝试将as 表示法与*ngIf 指令结合使用。

    这是你的做法https://stackblitz.com/edit/angular-8q7czz

    <p *ngIf="obs$ | async as num">
      {{  num > 5 ? '('+ num  +')' : num }}
    </p>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 2012-11-23
      • 1970-01-01
      • 2014-05-13
      • 2011-11-04
      • 2014-05-16
      • 2020-08-04
      相关资源
      最近更新 更多