通过ng-options中选择的角度跟踪ng-模型

时间:2015-10-29 21:28:26

标签: angularjs

所以我有:

<select class="form-control" 
        id="beneSelect" 
        ng-model="item.id as claimInfo.benefitId in claim"  
        ng-options="item.descr for item in claim track by item.id">
     <option value="">Please select a benefit....</option>
 </select>

我正在检索正确的东西,但没有推出正确的项目。似乎我需要/想要一个动态的ng模型和我的猎枪方法,一些研究没有按预期工作。尽管文档说的是什么,我还能在ng模型中没有表达式吗?在这种情况下,我得到id并希望保存为benefitID。

我这样的代码有效,但不提供属性,而是提供整个对象及其属性:

<select class="form-control" 
        id="beneSelect" 
        ng-model="claimInfo.benefitId" 
        ng-options="item.descr for item in claim track by item.id">
     <option value="">Please select a benefit....</option>
</select>

因此,claimInfo.benefitId应返回单个属性,它会返回我的JSON数据中的整个对象。

使用我的第二个代码示例,这就是我的JSON的样子:

[
  {
    "id": "",
    "benefitId": {
      "id": "kj5PdPr74MxR17HWaLs%2bFQ%3d%3d",
      "planTypeId": 4,
      "benefitTypeId": 14,
      "benefCode": "HRADED",
      "descr": "HRA Deductible (1/1/2015 - 12/31/2015)",
      "askSecIns": true,
      "askResidual": true,
      "hasFunds": true,
      "startDate": "2015-01-01T00:00:00",
      "endDate": "2015-12-31T00:00:00",
      "expenseTypes": [
        {
          "id": 94,
          "descr": "Deductible"
        }
      ],
      "dependents": [
        {
          "id": "kWOuvDLWk2965e6Y6cB4Tw%3d%3d",
          "name": "Yellow-TEST Ranger-TEST"
        },
        {
          "id": "%2fhCztDa9kyeJgjS61hB6yQ%3d%3d",
          "name": "Golden Ranger"
        },
        {
          "id": "R9Ml8nJlrhpj51fpuCN6gA%3d%3d",
          "name": "Beige Ranger"
        }
      ]
    },
    "isSecIns": "",
    "isNoResId": "",
    "expenseTypeId": "",
    "fromDate": "",
    "toDate": "",
    "provider": "",
    "who": "",
    "depId": "",
    "age": "",
    "amount": "",
    "comments": "",
    "isOffset": ""
  }
]

注意所有内容如何在benefitId属性下堆积而不是仅将属性保存到映射属性。

接下来的方法会更好,但是因为我的控制下面的ng-show没有按照我的努力进行切换,所以在此过程中会丢失一些东西。代码依赖于从上面控制中的ng-model集中提取的代码,假设整个对象被绑定为:

                <div class="form-group" ng-model="claimInfo.isSecIns" ng-show="claimInfo.benefitId.askSecIns == true" required>
                    <label for="secInc">Do you have secondary insurance</label>
                    <div>
                        <label class="radio-inline"><input type="radio" name="optradio">yes</label>
                        <label class="radio-inline"><input type="radio" name="optradio">no</label>
                    </div>
                </div>    

1 个答案:

答案 0 :(得分:0)

我认为您需要的是以下内容:

<select class="form-control" 
        id="beneSelect" 
        ng-model="claimInfo.benefitId" 
        ng-options="item.descr as item for item in claim">
     <option value="">Please select a benefit....</option>
</select>

将id存储在claimInfo.benefitId