AngularJS-ng-if检查字符串为空值 [英] AngularJS - ng-if check string empty value
本文介绍了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屋!
查看全文