AngularJS中的真假值是什么? [英] What are the truthy and falsy values in AngularJS?

查看:56
本文介绍了AngularJS中的真假值是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为将在ng-showng-class的上下文中考虑?还有其他情况吗?看来真相"和虚假"与纯JavaScript稍有不同-但到目前为止,我所知道的唯一一个例外是[]-这在JavaScript中是真相,但在AngularJS中是虚假".

I think it will be considered in the context of ng-show or ng-class? Are there other situations? It seems truthy and falsy are different from pure JavaScript slightly -- but the only one exception I know of so far is [] -- which is truthy in JavaScript but falsy in AngularJS.

(实际上,我尝试使用AngularJS 1.2.1和[]是虚假的,而在AngularJS 1.4.8和1.5.0上,[]是真实的.请参见https://jsfiddle.net/6ayaLhbk/ https://jsfiddle.net/6ayaLhbk/1/ https://jsfiddle.net/6ayaLhbk/2/)

(actually, I tried using AngularJS 1.2.1 and [] is falsy, while on AngularJS 1.4.8 and 1.5.0, [] is truthy. see https://jsfiddle.net/6ayaLhbk/ , https://jsfiddle.net/6ayaLhbk/1/ , and https://jsfiddle.net/6ayaLhbk/2/)

我认为可能有人会认为ng-show="ctrl.foo"ng-hide="!ctrl.foo"相同,但是在Angular 1.2.1的情况下却并非如此.要在Angular 1.2.1中获得纯JavaScript的相同结果,似乎我们可以使用ng-show="!!ctrl.foobar" https://jsfiddle.net/6ayaLhbk/3/

I think it has a catch that one may think ng-show="ctrl.foo" is the same as ng-hide="!ctrl.foo" but in the case of Angular 1.2.1, it is not. To get the identical result of pure JavaScript in Angular 1.2.1, seems like we can use ng-show="!!ctrl.foobar" https://jsfiddle.net/6ayaLhbk/3/

如果在Angular 1.2.1中为ng-class,则似乎可以追溯到纯JS真实和虚假规则: https://jsfiddle.net/6ayaLhbk/5/

If it is ng-class in Angular 1.2.1, then it seems like it goes back to the pure JS truthy and falsy rule: https://jsfiddle.net/6ayaLhbk/5/

推荐答案

以下值始终是虚假的:

  • false
  • 0(零)
  • "(空字符串)
  • 未定义
  • NaN(一个特殊的数字值,表示非数字!)

所有其他值均为真,包括"0"(引号为零),"false" (带引号的false),空函数,空数组和空对象.

All other values are truthy, including "0" (zero in quotes), "false" (false in quotes), empty functions, empty arrays, and empty objects.

var a = !!(0); // variable is set to false
var b = !!("0"); // true

比较虚假值 虚假的值遵循一些比较奇怪的比较规则,这可能会导致程序逻辑错误.

Comparing Falsy Values Falsy values follow some slightly odd comparison rules which can lead to errors in program logic.

伪造的值false,0(零)和"(空字符串)都是等效的,可以相互比较:

The falsy values false, 0 (zero), and "" (empty string) are all equivalent and can be compared against each other:

var c = (false == 0); // true
var d = (false == ""); // true
var e = (0 == ""); // true

虚假的null和undefined值与它们本身不等同:

The falsy values null and undefined are not equivalent to anything except themselves:

var f = (null == false); // false
var g = (null == null); // true
var h = (undefined == undefined); // true
var i = (undefined == null); // true

最后,假值NaN不等于任何东西-包括NaN!

Finally, the falsy value NaN is not equivalent to anything — including NaN!

var j = (NaN == null); // false
var k = (NaN == NaN); // false

您还应该知道typeof(NaN)返回数字".幸运的是,核心JavaScript函数isNaN()可用于评估值是否为NaN.

You should also be aware that typeof(NaN) returns "number". Fortunately, the core JavaScript function isNaN() can be used to evaluate whether a value is NaN or not.

如有疑问…… 在真值或假值可能导致逻辑错误的情况下,请使用严格等于(===)和严格不等于(!==).这些运算符可确保按类型和值对对象进行比较.

If in doubt… Use strict equal (===) and strict not equal (!==) in situations where truthy or falsy values could lead to logic errors. These operators ensure that the objects are compared by type and by value.

var l = (false == 0); // true
var m = (false === 0); // false

这篇关于AngularJS中的真假值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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