【问题标题】:Angular - Ternary operator with a ternary operator responseAngular - 具有三元运算符响应的三元运算符
【发布时间】:2021-08-01 20:04:11
【问题描述】:

我的 Angular HTML 中有一个三元运算符。我在格式化时遇到困难。如果该值等于文件长度,它应该返回Good,如果不需要我需要检查一些自定义值。我检查自定义值是否存在 (customs?.album_title),然后确定要显示的内容。如果两个选项都是字符串,它完全可以正常工作。但是,我需要 My Text 预先添加 customs.album_title 值,但我不完全确定该怎么做。正如您在下面的示例中看到的(这自然是不正确的),结果将是 customs?.album_title 作为字符串,而不是 My Text + 无论 customs.album_title 值是什么。

任何帮助将不胜感激。

{{ value == files.length ? 'Good' : customs?.album_title ? 'My Text customs.album_title' : 'String Two' }}

【问题讨论】:

  • 'My Text customs.album_title' -> 'My Text ' + customs.album_title?
  • 谢谢弗拉兹。恐怕我试过没有运气。
  • @Que “没有运气”是什么意思???那完全可以。
  • 谢谢。很抱歉,它没有奏效。当我尝试这样做时,Angular 不会构建。很明显,我希望它能够正常工作。
  • @Que 那你会遇到什么错误?

标签: javascript angular conditional-operator


【解决方案1】:

也许你的意思是这样的(使用 ${var})

{{ value == files.length ? 'Good' : customs?.album_title ? `My Text ${customs.album_title}` : 'String Two' }}

我不确定,但我认为 Angular 在 HTML 模板上尚不支持 Template Literals

所以现在,你可以尝试通过字符串连接来解决它

{{ value == files.length ? 'Good' : customs?.album_title ? ('My Text ' + customs.album_title) : 'String Two' }}

【讨论】:

    【解决方案2】:

    你可以试试这个:

    {{ value == files.length ? 'Good' : (customs?.album_title ? (('My Text ') + customs.album_title) : 'String Two') }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 2016-07-02
      • 2021-10-06
      相关资源
      最近更新 更多