检查null,empty或undefined angularjs [英] check null,empty or undefined angularjs

查看:198
本文介绍了检查null,empty或undefined angularjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angularjs创建一个项目.我有像这样的变量

I am creating a project using angularjs.I have variable like

$scope.test = null
$scope.test = undefined
$scope.test = ""

我想在一种情况下检查所有null,undefined和empty值

I want to check all null,undefined and empty value in one condition

推荐答案

只需使用-

if(!a) // if a is negative,undefined,null,empty value then...
{
    // do whatever
}
else {
    // do whatever
}

之所以起作用,是因为javascript中===与===的区别在于,它会将某些类型的值转换为其他类型的相等"值以检查是否相等,而===则相反,后者仅检查这些值是否相等.因此,基本上==运算符知道将未定义的",null转换为假值.正是您所需要的.

this works because of the == difference from === in javascript, which converts some values to "equal" values in other types to check for equality, as opposed for === which simply checks if the values equal. so basically the == operator know to convert the "", null, undefined to a false value. which is exactly what you need.

这篇关于检查null,empty或undefined angularjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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