Search code examples
javascriptangularjsangularjs-ng-options

Angular ng-model with ng-options


Having trouble setting the default option in my ng-options/ng-model.

My problem is that while the dropdown is getting populated with the correct options, the default option is blank. This leads me to believe there's something wrong with how I'm doing ng-model. Any help?

I have a table I'm using ng-repeat to iterate over an array:

$scope.dumpsters = [
    {id:"1", sub_customer: 'a'},
    {id:"2", sub_customer: 'b'},
    {id:"3", sub_customer: 'c'}
]

For simplicity, I have an array of sub_customers that is separate:

$scope.subCustomers = [{description:'a'}, {description:'b'}, {description:'c'}];

so now in my table, each row has a column that contains this:

<select ng-model="d.sub_customer" ng-options="v.description for v in subCustomers" ng-change="updateCustomer(d.sub_customer, d.id)"></select>

Solution

  • You can use select as in this case, issue is that with your syntax object references must match. Assuming the matching item is the description field you could just do:

    <select ng-model="d.sub_customer" 
         ng-options="v.description as v.description for v in subCustomers" 
         ng-change="updateCustomer(d.sub_customer, d.id)"></select>