[英]Using IF statements in Underscore.js/Backbone.js templates
我收到一个JSON对象,其中一个值为null
。 JSON看起来像:
[{"id":"1096","price":null,
现在,它使用以下代码向网页输出NULL
字符串。 (我在Backbone.js / Underscore.js中使用模板引擎)
<div class="subtitle">$<%= price %></div>
因为如果没有返回price
我想隐藏整个div
,我添加了if
语句:
<% if (price) { %>
<div class="subtitle">$<%= price %></div>
<% } %>
但是它似乎仍然输出div.subtitle
。 我究竟做错了什么? 我也试过以下但是没有用
<% if (typeof(price) != "undefined") { %>
<div class="subtitle">$<%= price %></div>
<% } %>
<% if (price != null) { %>
<div class="subtitle">$<%= price %></div>
<% } %>
<% if (price != "null") { %>
<div class="subtitle">$<%= price %></div>
<% } %>
我怀疑这与在Underscore.js模板中使用if
语句有关
呃,你不想要(没有感叹号)
<% if (price) { %>
<div class="subtitle">$<%= price %></div>
<% } %>
因为你现在说如果没有价格那么显示价格......这没有任何意义。
null
undefined
!
如果你的json-object被正确解码,那么检查(price)
或(price != null)
应该没问题。
不应该是==
(在这种情况下!==
)进行比较
<% if (price !== null) { %>
<div class="subtitle">$<%= price %></div>
<% } %>
例如,看看这个Jsbin.com线的警告
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.