Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

Here's the "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. 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.

Any suggestions?

Thank you!

like image 249
Naner Avatar asked Mar 04 '15 23:03

Naner


People also ask

What is difference between ng-repeat and Ng-options?

ng-repeat creates a new scope for each iteration so will not perform as well as ng-options. For small lists, it will not matter, but larger lists should use ng-options. Apart from that, It provides lot of flexibility in specifying iterator and offers performance benefits over ng-repeat.

How does ng select work?

The ng-selected directive sets the selected attribute of an <option> element in a <select> list. The option will be selected if the expression inside the ng-selected attribute returns true. The ng-selected directive is necessary to be able to shift the value between true and false .

What are ng-options?

Definition and Usage. The ng-options directive fills a <select> element with <options>. The ng-options directive uses an array to fill the dropdown list. In many cases it would be easier to use the ng-repeat directive, but you have more flexibility when using the ng-options directive.

What is the difference between Ng bind and NG model in AngularJS?

ngModel usually use for input tags for bind a variable that we can change variable from controller and html page but ngBind use for display a variable in html page and we can change variable just from controller and html just show variable. Save this answer.


1 Answers

The issue is that the "assistanceType" object in selectedRecord isn't the exact same instance of its equivalent in the assistanceTypes array. 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.

like image 72
HankScorpio Avatar answered Sep 21 '22 12:09

HankScorpio