AngularJS - ng-if 检查字符串空值 [英] AngularJS - ng-if check string empty value

查看:41
本文介绍了AngularJS - ng-if 检查字符串空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的例子:

<a ng-if="item.photo == ''" href="#/details/{{item.id}}"><img src="/img.jpg" class="img-responsive"></a>
<a ng-if="item.photo != ''" href="#/details/{{item.id}}"><img ng-src="/{{item.photo}}" class="img-responsive"></a>

我看到它总是生成 item.photo != '' 条件,即使该值为空.为什么?

I see it always generates the item.photo != '' condition even if the value is empty. Why?

推荐答案

你不需要明确使用像 item.photo == ''item.photo != 这样的限定符''.就像在 JavaScript 中一样,空字符串将被评估为 false.

You don't need to explicitly use qualifiers like item.photo == '' or item.photo != ''. Like in JavaScript, an empty string will be evaluated as false.

您的视图也将更加清晰和可读.

Your views will be much cleaner and readable as well.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>
<div ng-app init="item = {photo: ''}">
   <div ng-if="item.photo"> show if photo is not empty</div>
   <div ng-if="!item.photo"> show if photo is empty</div>
  
   <input type=text ng-model="item.photo" placeholder="photo" />
</div

更新以消除 Angular 中的错误

这篇关于AngularJS - ng-if 检查字符串空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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