【问题标题】:How to show the tooltip value based on condition using angular如何使用角度根据条件显示工具提示值
【发布时间】:2021-09-21 18:31:32
【问题描述】:

如何为列传输显示一些值为 0 / 1 的文本,例如,如果值为 0,则工具提示上的文本应“激活”,如果值为 1,则文本应“未激活”

以下是我编写的逻辑,但它不起作用,请建议我正确的更改。

<span *ngIf = "headerDef['property'][i] === 'transfer' "
title="{{headerDef['transfer'][i] == '0' ? headerDef['transfer'][i]: 'Activated' }}
{{ headerDef['transfer'][i] == '0' ? headerDef['transfer'][i]: 'Not Activated' }}"
>
</span>

【问题讨论】:

  • (1.) 0/1 !== 布尔值。 (2.) 在您与字符串'0' 进行比较的情况下。这有很大的不同。 !!0 === false!!'0' === true。那么正确的条件是什么?
  • @MichaelD 是字符串类型,而不是布尔类型
  • @MichaelD 但它仍然在工具提示中显示 0
  • 我在答案中发布了一个工作示例Stackblitz

标签: html angular typescript angular8


【解决方案1】:

考虑到您的评论(与字符串比较),您可以使用单个三元运算符。

试试下面的

<span 
  *ngIf="headerDef['property'][i] === 'transfer'"
  [title]="headerDef['transfer'][i] == '0' ? 'Activated' : 'Not Activated'"
>
</span>

请注意,我们假设如果值为'0',则必须'1'并显示'Not Activated'。如果有额外的值需要检查,则需要扩展三元运算符。

更新: 工作示例:Stackblitz

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-27
    • 2018-06-28
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多