Isu*_*uru 14 angularjs ng-switch
我想用角度ng-switch检查布尔数据
这是我的代码.但它不起作用
<div ng-switch={{Item.ItemDetails.IsNew}}>
<div ng-switch-when="true">
<p class="new fontsize9 fontWeightBold">NEW</p>
</div>
</div>
<div ng-switch={{Item.ItemDetails.IsFeatured}}>
<div ng-switch-when="true">
<div class="featured">
<p class="fontWeightBold fontsize8">featured</p>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
{{Item.ItemDetails.IsNew}}和{{Item.ItemDetails.IsFeatured}}的值为true或false
小智 22
Convert the boolean to a string:
<div ng-switch="Item.ItemDetails.IsNew.toString()">
<div ng-switch-when="true">
Run Code Online (Sandbox Code Playgroud)
Odd*_*say 15
如果您只是检查真值,ng-if似乎更合适,并减少了包含代码的其他div的需要,也减少了样本:
<div ng-if="Item.ItemDetails.IsNew">
<p class="new fontsize9 fontWeightBold">NEW</p>
</div>
<div class="featured" ng-if="Item.ItemDetails.IsFeatured">
<p class="fontWeightBold fontsize8">featured</p>
</div>
Run Code Online (Sandbox Code Playgroud)
完整文档:http://docs.angularjs.org/api/ng.directive:ngIf
这种语法对我有用:
<div ng-switch="Item.ItemDetails.IsFeatured">
<div ng-switch-when="true">FEATURED ITEM HTML</div>
<div ng-switch-default>REGULAR ITEM HTML (not featured)</div>
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22396 次 |
| 最近记录: |