Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Plunkr: http://plnkr.co/edit/RgNcSg?p=preview

I have a drop down of "locations" which is populated by an object array. If I set the object value, which I am doing in my controller, it is not selected in the dropdown, why not? You can see by the html output that the binding works and the value is set properly, but the drop down box cannot be set from the controller.

Controller:

var myapp = angular.module('myapp', []);

myapp.controller('ctrl', function($scope) {
  $scope.locationOptions = [
    {id: 1, name: "option 1"},
    {id: 2, name: "option 2"}];
  $scope.options = {
    location: { id:1, name: "option 1"}
  }
});

Template:

<div class="form-group">
    <label for="location" class="control-label col-md-2">Location</label>
    <div class="col-md-3">
        <select id="location"
                class="form-control"
                ng-options="loc.name for loc in locationOptions"
                ng-disabled="loadingLocations"
                ng-model="options.location">
        </select>
    </div> 
</div>
LocationID: {{options.location.id}}
Location: {{options.location.name}}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

This is because angularjs uses references to link, try doing this instead:

 $scope.options =  $scope.locationOptions[0]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.