【问题标题】:Angular2 pipes under condition, check typeAngular2管道处于条件下,检查类型
【发布时间】:2017-02-05 08:27:16
【问题描述】:

我在here条件下找到了管道

如何检查类型是数字并给它管道?

{{(item).isNumber ? (item | currency: 'USD':true:'1.2-2') : (item)}}

像这样。

ps。我不想使用自定义管道装饰。

有什么好主意吗?谢谢

【问题讨论】:

    标签: angular angular2-pipe


    【解决方案1】:

    您只能使用组件类中可用的方法和对象。因此,字符串插值中没有可用的原生 Javascript 函数。

    但是,您可以在组件中编写辅助方法(取自 this post):

    isNumber(o): boolean {
      return ! isNaN (o-0) && o !== null && o !== "" && o !== false;
    }
    

    并像这样使用它:

    {{ isNumber(item) ? (item | currency: 'USD':true:'1.2-2') : (item) }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 2023-02-09
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 2010-11-13
      相关资源
      最近更新 更多