![](/img/trans.png)
[英]How to display placeholders in AngularJS for undefined expression values?
[英]How to get an AngularJS Expression to display nothing?
說我有以下HTML:
<p>{{ booleanVariable ? "The variable is true!" : null }}</p>
如果booleanVariable
為false,則表達式應為空,並且<p></p>
不應呈現。 但是,它只是將“ null”打印到<p>
標記中,就像我剛剛鍵入<p>null</p>
。
我將如何使表達式不呈現任何內容?
使用undefined
代替null
。
<p>{{ booleanVariable ? "The variable is true!" : undefined }}</p>
一個更簡單的解決方案是ngShow
:
<p ng-show="booleanVariable">The variable is true!</p>
最好的解決方案是使用
<p ng-if="booleanVariable">The variable is true!</p>
ng-show
將有<p>
在DOM但它只是隱藏的,如果你使用ng-if
<p>
將不會在DOM還
空字符串是有意義的。
<p>{{ booleanVariable ? "The variable is true!" : '' }}</p>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.