Angular ng-show选择选项子选项

时间:2015-10-08 21:55:40

标签: javascript json angularjs drop-down-menu

所以,如果我有一个选择:

<div class="form-group">
    <label for="beneSelect">Select your benefit</label>
    <select class="form-control" id="beneSelect" >
        <option ng-repeat="descr in claim.claimBenes" 
                data-ng-model="claimInfo.providerName">{{ descr.descr }}</option>
    </select>
</div>

在此之下,如果&#39; descr&#39;有一个包含数据的兄弟属性,ng-show一个选择option,最好的方法是什么或最好的方式?

例如,如果我提取的 JSON 数据有9个不同的属性,并且在初始选择中我显示如下:

  "id": "%2fooTA9gmtHE8IJ13CdcAww%3d%3d",
  "planTypeId": 1,
  "benefitTypeId": 11,
  "benefCode": "LHCFSA",
  "descr": "Limited Health Care FSA (1/1/2015 - 12/31/2015)",
  "askSecIns": false,
  "askResidual": false,
  "hasFunds": true,
  "startDate": "2015-01-01T00:00:00",
  "endDate": "2015-12-31T00:00:00",
  "expenseTypes": [
    {
      "id": 56,
      "descr": "General Dental Care"
    },
    {
      "id": 52,
      "descr": "General Vision Care"
    },
    {
      "id": 57,
      "descr": "Orthodontia"
    },
    {
      "id": 58,
      "descr": "Preventive Care"
    }
  ],

如果expenseTypes&#34; HAS&#34;数据,然后另一个选择将显示,否则,只有一个选择将显示。实际上,我会在其中使用另一个<div>显示带有HTML的新<select>

不确定在Angular中解决此问题的最佳方法。请举例说明吗?

非常感谢。

2 个答案:

答案 0 :(得分:0)

可以使用expenseTypes数组的长度作为布尔值

ng-if="claim.expenseTypes.length"

答案 1 :(得分:0)

我猜你正在寻找这个......

也许它可以帮助你提出一些想法......

(function() {
  'use strict';

  function InputController() {
    var secondary = {
        A: [1, 2, 3],
        B: [3, 4, 5]
      },
      vm = this;

    vm.primary = ['A', 'B'];
    vm.selectedPrimary = vm.primary[0];

    vm.onPrimaryChange = function() {
      vm.secondary = secondary[vm.selectedPrimary];
    };
  }

  angular.module('inputs', [])
    .controller('InputCtrl', InputController);

}());
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script>
<div class="container" ng-app="inputs" ng-controller="InputCtrl as ctrl">
  <div class="row">
    <div class="col-xs-6">
      <h3>Primary</h3>
      <select class="form-control" ng-model="ctrl.selectedPrimary" ng-options="item for item in ctrl.primary" ng-change="ctrl.onPrimaryChange()"></select>
    </div>
    <div class="col-xs-6">
      <h3>Secondary</h3>
      <select class="form-control" ng-model="ctrl.selectedSecondary" ng-options="item for item in ctrl.secondary"></select>
    </div>
  </div>
</div>