javascript - AngularJS ng-重复 : show data only when data is not false

标签 javascript json angularjs

我在应用程序中使用 Angular,并在 html 页面中显示数据,如下所示:

<div class="orderspresent" ng-cloak="">
 <div class="roworders" ng-controller="allorders" >
  <div class="col-md-3" ng-repeat= "o in orders">
   <div class="sm-st clearfix">
    <div class="sm-st-info">
     <i class="fa fa-square"></i>
      <span>{{o.customerName}}</span>
      <p>1 minutes</p>
      <ul>
        <li ng-repeat= "details in o.details">
        {{details.aantal}} x {{details.productTitle}}
        <br>+ {{details.extras}} 
        </li>     
     </ul>
    </div>
   </div>
  </div>             
 </div>
</div>

JSON 示例

   {
    "49": {
           "orderID": 49,
           "customerID": 61,
           "orderDate": "2015-05-06 10:03:05",
           "orderDeliveryDate": "2015-05-06",
           "orderStatus": "prepare",
           "customerName": "Caroline",
           "details": [
                       {
                         "orderdetailID": 83,
                         "productTitle": "Sexy Teacher",
                         "productPrijs": 4,
                         "aantal": 2,
                         "extras": "Extra syrup, Soya milk",
                         "extrasPrice": 0.6
                       },
                       {
                         "orderdetailID": 84,
                         "productTitle": "Caramel Macchiato",
                         "productPrijs": 5.2,
                         "aantal": 2,
                         "extras": false,
                         "extrasPrice": 0
                      },
                      {
                         "orderdetailID": 85,
                         "productTitle": "The Coach",
                         "productPrijs": 3,
                         "aantal": 3,
                         "extras": false,
                         "extrasPrice": 0
                     },
                     {
                        "orderdetailID": 86,
                        "productTitle": "The Virgin",
                        "productPrijs": 3.2,
                        "aantal": 5,
                        "extras": "Extra syrup, Whipped cream, 3 espresso shots",
                        "extrasPrice": 2.4
                     }
                    ]
                }
    }

正如您在上面的 JSON 示例中看到的,{{details.extras}} 值有时可能为 false。所以现在当我使用 {{details.extra}} 时,它在我的 html 中显示 false。但在这种情况下,我不想在那里显示任何内容。只是空白。

如何实现这一点?

最佳答案

最好的选择是使用 ng-if :

<span ng-if="details.extras">+ {{details.extras}}<span>

ng-if 从 DOM 中删除节点。更小的 DOM - 更快的操作。

关于javascript - AngularJS ng-重复 : show data only when data is not false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30074057/

相关文章:

javascript - 将 Boomerang 实现为浏览器扩展/插件

javascript - jquery验证错误

java - JSON Jackson将json中的文件路径(字符串)反序列化为java中的File对象

javascript - 如何使用正则表达式在 angularjs 上打印值?

javascript - 在自定义 JS 光标上设置光标原点

javascript - 如何在不刷新的情况下显示php传递的结果给ajax

android - 是否可以查询存储在 Android SQLite 数据库中的 JSON 数据

jquery - 从 json 文件中提取数据以在 Jquery 中的 .$post 中使用

javascript - 将类添加到 AngularJs 中的多个元素

javascript - Angularjs是基于哪种设计模式?,学习一种设计模式就够了?