Javascript 如何在angularjs中检查字符串是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36869983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:39:28  来源:igfitidea点击:

how to check a string is empty in angularjs

javascript

提问by hanu

I am checking a value in my controller.Here is my code.

我正在检查我的控制器中的一个值。这是我的代码。

var contents = atob(response.documents);
// I checked the value in contents . it is displaying ""
if(contents === ""){
 // no contents
}

the if condition is retuning false, when the contents has "" and when the contents has data. How to check condition?

if 条件重新调整为 false,当内容有 "" 并且内容有数据时。如何检查条件?

回答by tpsilva

Good plain Javascript.

良好的纯 Javascript。

if(!contents){

}

回答by juandemarco

How about contents.trim().length === 0?

怎么样contents.trim().length === 0

回答by Revive

Empty string in Javascript is falsey you can use:

Javascript 中的空字符串是错误的,您可以使用:

if(contents){
  //has content
}else{
 //no content
}

回答by Hazarapet Tunanyan

In javascript "" is false and when you compare it with "", it alwaysgives true.Maybe you have doubled empty string (empty string inside string)

在javascript中“”是假的,当你将它与“”进行比较时,它总是给出。也许你已经加倍了空字符串(字符串中的空字符串)