角度 4: *ngIf 有多个条件

Posted

技术标签:

【中文标题】角度 4: *ngIf 有多个条件【英文标题】:angular 4: *ngIf with multiple conditions 【发布时间】: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 【参考方案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'

以上是关于角度 4: *ngIf 有多个条件的主要内容,如果未能解决你的问题,请参考以下文章

尝试为 RouterOutlet 和 NgIf 配置 _source 的角度和错误

角度 2 ngIf 与可观察?

如何使用角度根据条件显示工具提示值

角度 6 动画 *ngIf,过渡不起作用

html 角度5 ngIf / else

角度模板 ngif 在重新加载页面之前不起作用