简体   繁体   中英

Angular JS: show item only if ng-repeat has members?

I have json data where a series of objects have an array "options". Some items have items in this array and some don't, as in

{
  "label": "ORDERS",
  "enabled": true,
  "selected": true,
  "options": [
    {
      "label": "Edit addresses",
      "value": true
    },
    {
      "label": "Cancel orders",
      "value": true
    },
    {
      "label": "Uncancel orders",
      "value": false
    }
  ]
},
{
  "label": "FOO",
  "enabled": true,
  "selected": false,
  "options": [

  ]
},

As you can see, ORDERS has options, but FOO does not.

This leads to a layout like:

在此处输入图片说明

I have items outside the repeater data, the Allowed header and checkbox.

I only want to show those when the options array has items. Here's the way I laid it out: http://plnkr.co/edit/QLY9311r4nSCRUolKvLs?p=preview

I can't figure out how to hide the rows with the "Allowed" header and checkbox on the rows where there is no options array:

在此处输入图片说明

Will this work?

    <tr ng-if="t.options.length">
      <td colspan="2"></td>
      <td align="center">Allowed</td>
    </tr>
    <tr ng-if="t.options.length">
      <td colspan="2"></td>
      <td align="center">
        <input type="checkbox" name="" id="" />
      </td>
    </tr>
    <tr ng-repeat="o in t.options">
      <td></td>
      <td>{{o.label}}</td>
      <td align="center">
        <input type="checkbox" ng-model="o.selected" />
      </td>
    </tr>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM