简体   繁体   English

选择不与ng-model和ng-options绑定时选择

[英]Select not selecting when binding with ng-model and ng-options

Here's my select: 这是我的选择:

<select class="form-control" ng-options="assistanceType as assistanceType.name for assistanceType in assistanceTypes" ng-model="selectedRecord.assistanceType"></select>

Here's what I'm using to load the Assistance Types: 这是我用来加载援助类型的内容:

$scope.getAssistanceTypes = function () {
    $http.get('/api/assistanceType/getAll').
    success(function (data, status, headers, config) {
        $scope.assistanceTypes = data;
    }).
    error(function (data, status, headers, config) {
        alert(data.ExceptionMessage);
    });
}

Here's the result: 这是结果:

    [
  {
    "assistanceTypeId": 1,
    "name": "Essay"
  },
  {
    "assistanceTypeId": 2,
    "name": "Resume"
  },
  {
    "assistanceTypeId": 3,
    "name": "Test"
  }
]

Everything works fine and I can see the model being updated as I change options. 一切正常,我可以在更改选项时看到模型正在更新。

But when I load the record ($scope.selectedRecord), the selected option does not reflect the assistanceType object! 但是当我加载记录($ scope.selectedRecord)时,所选选项不会反映aidType对象!

Here's the "selectedRecord": 这是“selectedRecord”:

{
  "recordId": 1,
  "student": {
    "id": "xxx",
    "firstName": "xxx",
    "lastName": "xxx"
  },
  "createDate": "2015-03-04T15:35:40",
  "closeDate": "2015-03-04T15:35:40",
  "checkInDate": "2015-03-04T15:35:40",
  "checkOutDate": "2015-03-04T15:35:40",
  "consultant": {
    "id": "xxx",
    "firstName": "xxx",
    "lastName": "xxx"
  },
  "assistanceType": {
    "assistanceTypeId": 1,
    "name": "Essay"
  },
  "course": {
    "course": "xxx",
    "name": "xxx",
    "teacher": {
      "id": "xxx",
      "firstName": "xxx",
      "lastName": "xxx"
    }
  },
  "format": null,
  "classStanding": null,
  "comment": "Nothing here!"
}

I'm new to AngularJS and I could very well be missing something here. 我是AngularJS的新手,我很可能在这里遗漏了一些东西。 But it looks like to me that at the moment I load the main record, the select should populate with the object in selectedRecord.assistanceType. 但是在我看来,在我加载主记录时,select应该填充selectedRecord.assistanceType中的对象。

Any suggestions? 有什么建议么?

Thank you! 谢谢!

The issue is that the "assistanceType" object in selectedRecord isn't the exact same instance of its equivalent in the assistanceTypes array. 问题是selectedRecord中的“assistanceType”对象与aidTypes数组中的等效实例不完全相同。 Try this: 尝试这个:

<select class="form-control" ng-options="assistanceType as assistanceType.name for assistanceType in assistanceTypes track by assistanceType.name" ng-model="selectedRecord.assistanceType"></select>

Note that I added "track by assistanceType.name" so that it will compare them by name instead of the object's $$hashkey. 请注意,我添加了“track by aidType.name”,以便它将按名称而不是对象的$$ hashkey进行比较。

Select element require name attribute to get model element select, in your case just add add name attribute. 选择element require name属性来获取模型元素选择,在你的情况下只需添加add name属性。 For example name="assistanceType". 例如name =“aidType”。 There is no need to add track by untill its require. 除了它的要求之外,无需添加轨道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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