Angular 2:ngIf指令中比较运算符==和===之间有什么区别 [英] Angular 2: what differences between comparison operators == and === in ngIf directive

查看:246
本文介绍了Angular 2:ngIf指令中比较运算符==和===之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么这两个运算符存在.在布尔比较的情况下,==和===似乎都起作用,但是在枚举比较的情况下,仅'=='有效:

I don't understand why these two operators exist. In case of boolean comparison both == and === seem to work, but in case of enum comparison only '==' works:

<div class="interventionGroup">

    <div class="interventionGroupHeader transition_1s" (click)="onClickHeader()">
        {{GroupName}}
        <div *ngIf="expanded == true" class="expand-icon"><i class="material-icons">expand_less</i></div>  <!-- WORKS -->
        <div *ngIf="expanded === false" class="expand-icon"><i class="material-icons expand-icon">expand_more</i></div> <!-- WORKS -->
    </div>

    <button *ngIf="GroupType == GroupTypeEnum.mesInterventions">dfdsfsd</button> <!-- WORKS -->

    <div style="list-style-type:none" *ngIf="expanded === true">
        <div *ngFor="let intervention of interventions"
            (click)="onClick(intervention)"> 
            <intervention-button [intervention]="intervention"></intervention-button>
        </div>
    </div>
</div>

推荐答案

在javascript中,运算符'=='仅检查相等性,而'==='检查类型和相等性

In javascript, the operator '==' only check equality and '===' check type and equality

0 == '0' => true
0 === '0' => false

这篇关于Angular 2:ngIf指令中比较运算符==和===之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆