【问题标题】:if else condition in angular html tag with "or"if else条件在带有“或”的角度html标签中
【发布时间】:2018-06-21 22:41:53
【问题描述】:

我必须在 html 中显示元素的数量 我希望它在 n>1 或 n = 0 时显示“n 个学生参加” 和 1 名学生参加”当它是唯一的一个时:n = 1 名学生。 所以我在 html 中创建了这个:

<a href="#"><h6>
    <strong>{{group.students.length}}
      {{group?.students?.length > 1 ? "Students attended" :
      "student attended" }}</strong></h6></a>

它工作正常。但是当我像这样尝试 n=0 时的条件时:

<a href="#">
 <h6>
   <strong>{{group.students.length}}
  {{(group?.students?.length > 1 || group?.students?.length == 0 ) ? "Students attended" :
  "student attended" }}</strong>
 </h6>
</a>

这不起作用。如何在 if 条件中添加“或”?

也许这对某些人来说似乎很明显,但我对ionic 3 完全陌生,所以如果这是一个愚蠢的问题,我很抱歉。

谢谢。

【问题讨论】:

  • 你说的不工作是什么意思?你得到了什么?
  • 您可以通过仅使用一个条件来实现它,即 group?.students?.length == 1 因为对于所有其他情况,您希望 n 个学生参加。
  • 我刚刚测试了你的代码。它完全有效。
  • 非常感谢@RachitShroff!也许您可以将其发布为答案,以便我投票:D 非常感谢 :)))

标签: html angular if-statement ionic3


【解决方案1】:

您可以只使用一个条件直接实现它,即 group?.students?.length == 1,因为对于所有其他情况,您希望 n 个学生参加。

所以你的更新代码将是:

<a href="#">
 <h6>
   <strong>
     {{group.students.length}}
     {{group?.students?.length == 1  ? "student attended" : "Students attended" }}
</strong>
 </h6>
</a>

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    相关资源
    最近更新 更多