Angular ng-if 子句不能双向工作

Angular ng-if clause not working both ways

我在 table 中显示 json,像这样:

<tr ng-repeat="room in allRooms | orderBy: 'OpenTime'">

其中 allRooms 包含 json 个像这样的对象:

{
  "Name": "Room five",
  "OpenTime": "2016-02-19 00:00:00",
  "Entries": 2,
  "Capacity": 10,
  "Type": "TypeThree"
}

并且类型 属性 可以是五个不同值之一("typeOne"、"typeTwo"、"TypeThree" 等)

我想使用 ng-if 在列中显示按钮,前提是类型不是 "typeOne" 或 "typeTwo",例如:

<button type="button" ng-if="room.Type != 'typeOne' || room.Type != 'typeTwo'"></button>

这对我不起作用,尽管它正是我所需要的。

但是,这个(虽然它没有达到我需要的)确实有效:

<button type="button" ng-if="room.Type == 'typeOne' || room.Type == 'typeTwo'"></button>

所以我想知道为什么第二个 ng-if 可以正常工作,而第一个却不能,因为两者之间的唯一区别是使用等于或不等于运算符?

这里的问题出在你的逻辑上。在不知道 allRooms 的数据中包含什么的情况下,我可以使用数字来最好地说明为什么第一个 if 语句很奇怪。假设 if 语句如下:

if (a !== 1 || a !== 2)

if 语句永远为真。否则不能,因为如果值为 1,则不是 2,如果值为 2,则不是 1。所以第一个 if 语句的问题是它总是计算为 true,永远不会为 false。

您可能想要的是等价于此:

if (a !== 1 && a !== 2)

在这种情况下,if 语句将通过小于 1 和大于 2 的整数,而对于 1 或 2 将失败。事实上,它可以通过任何值,无论是否为整数!