在AngularJS中的Select tag中选择一个项目

时间:2017-08-28 10:12:37

标签: html angularjs

我不明白为什么我的价值没有被选中。

我有一个editItem(对象类型是Entity),其中一个字段包含另一个项目的名称(对象类型赞助商)。

在下面的示例中,Sponsor_name是赞助商对象的名称

这是我的模型("实体"对象)

{
  "id": 130,
  "Entity_name": "RH-commun",
  "Entity_sponsor": "AUFC",
  "Entity_billing_code": "137"
} 

以下是赞助商列表(subObjectList)

[
 {
  "id": 1,
  "objectType": "Sponsor",
  "content": "{\"id\": 376316, \"Sponsor_name\": \"AUFC\"}",
  "previewContent": {
    "id": 1,
    "Sponsor_name": "AUFC"
  },
 "relationNumber": null
 },
 {
  "id": 2,
  "objectType": "Sponsor",
  "content": "{\"id\": 376317, \"Sponsor_name\": \"B3G\"}",
  "previewContent": {
  "id": 2,
  "Sponsor_name": "B3G"
  },
 "relationNumber": null
 }
]

现在这是我的Angular代码中的Html

$scope.form += '<select class="form-control"  id="' + keyname + '" name="' + keyname + '" type="text"  ng-model="editItem.previewContent.' + keyname + '"  ng-options="item.previewContent.' + childName + ' for item in subObjectList track by item.previewContent.' + childName + '"  ng-required="' + isRequired + '"  /> '

keyname等于"Entity_sponsor"childname等于"Sponsor_name"

这是select的最终生成的html:

<select class="form-control ng-pristine ng-empty ng-invalid ng-invalid-required ng-touched" id="Entity_sponsor" name="Entity_sponsor" type="text" ng-model="editItem.previewContent.Entity_sponsor" ng-options="item.previewContent.Sponsor_name for item in subObjectList track by item.previewContent.Sponsor_name" ng-required="true" required="required">
    <option value="?" selected="selected"></option><option label="AUFC" value="AUFC">AUFC</option>
    <option label="B3G" value="B3G">B3G</option><option label="B-DIG" value="B-DIG">B-DIG</option>
    <option label="BEEI" value="BEEI">BEEI</option><option label="BEF" value="BEF">BEF</option>
    <option label="BIN" value="BIN">BIN</option><option label="DEC" value="DEC">DEC</option><option label="DGAZ" value="DGAZ">DGAZ</option><option label="DGF" value="DGF">DGF</option>
    <option label="DGR" value="DGR">DGR</option><option label="DOAAT" value="DOAAT">DOAAT</option><option label="DPNT" value="DPNT">DPNT</option><option label="DSP" value="DSP">DSP</option>
    <option label="ERD" value="ERD">ERD</option><option label="ENEDIS" value="ENEDIS">ENEDIS</option>
    <option label="FCC" value="FCC">FCC</option><option label="FIL" value="FIL">FIL</option>
    <option label="GRDF" value="GRDF">GRDF</option><option label="PREST" value="PREST">PREST</option>
    <option label="RTE" value="RTE">RTE</option><option label="SEI" value="SEI">SEI</option>
    <option label="SG" value="SG">SG</option><option label="TIE" value="TIE">TIE</option>
    <option label="DPIH" value="DPIH">DPIH</option><option label="DIPNN" value="DIPNN">DIPNN</option>
    <option label="DISP" value="DISP">DISP</option><option label="DI" value="DI">DI</option>
</select>

因此,对于每个实体项目,如果我正在编辑一个项目,则绑定到实体对象的赞助商的选择为空。此列表由赞助商项目填写,但无法获得选定的值

1 个答案:

答案 0 :(得分:0)

找到解决方案:

替换

class Nayan
{
    Employees n1 = new Employees();

    public Nayan()
    {
        n1.Id = 2;
    }
}

通过

ng-options="item.previewContent.' + childName + ' for item in subObjectList track by item.previewContent.' + childName + '"  ng-required="' + isRequired + '"  /> '