【问题标题】:angular 4: *ngIf with multiple conditions角度 4: *ngIf 有多个条件
【发布时间】:2017-10-03 18:01:45
【问题描述】:

我有点困惑。如果结果有几种情况之一,我需要隐藏块。但似乎它无法正常工作......

<div *ngIf="currentStatus !== 'open' || currentStatus !== 'reopen' ">

    <p padding text-center class="text-notification">{{message}}</p>

</div>

它只是以其他条件出现。它既不适用于 1 条件,也不适用于 2。还尝试了 *ngIf="currentStatus !== ('open' || 'reopen') ",但它仅适用于 1 种情况。

【问题讨论】:

  • 预期的行为是什么?什么时候应该评估为true,什么时候应该评估为false

标签: angular typescript angular2-directives angular-ng-if


【解决方案1】:

除了多余的 ) 之外,此表达式将始终为 true,因为 currentStatus 将始终匹配以下两个条件之一:

currentStatus !== 'open' || currentStatus !== 'reopen'

也许你的意思是其中之一

!(currentStatus === 'open' || currentStatus === 'reopen')
(currentStatus !== 'open' && currentStatus !== 'reopen')

【讨论】:

  • 那么根据什么状态,预期的行为是什么?
  • @Merge-pony 看来您使用的是 OR 而不是 AND,因为 !(currentStatus === 'open' || currentStatus === 'reopen') 给出的结果与 currentStatus !== 'open' &amp;&amp; currentStatus !== 'reopen' 相同
  • @mankers 是的,是的!!!但它不起作用。我正在使用 Ionic 3 和 Angular 4 堆栈...
【解决方案2】:

你有一个忍者')'。

试试:

<div *ngIf="currentStatus !== 'open' || currentStatus !== 'reopen'">

【讨论】:

  • 在示例中犯了错误。我的错。按照要求在原始代码结果中
【解决方案3】:
<div *ngIf="currentStatus !== ('status1' || 'status2' || 'status3' || 'status4')">

【讨论】:

  • 它到底怎么会有 4 个赞? ('status1' || 'status2' || 'status3' || 'status4') 就是 'status1'
猜你喜欢
  • 1970-01-01
  • 2019-01-27
  • 2018-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-25
  • 2019-03-31
  • 2018-04-19
相关资源
最近更新 更多