Search code examples
angularjsng-optionsangularjs-ng-options

Using ng-options on array declared in an object


In my controller, I have:

 $scope.sizes = {
    things: [
        'one',
        'two'
        ]
}

and in my HTML, I have

  <select ng-options="thing for thing in sizes.things"></select>

The resulting combobox has no values. However, in this case:

 $scope.things = ['one', 'two'];

  <select ng-options="thing for thing in things"></select>

The resulting combobox shows "one", "two" as its options. Can someone explain why I can't access the array within a $scope object?


Solution

  • You were missing the ng-model attribute on your ng-options select element.

    Adding ng-model="thing" gets it working...

    <select ng-model="thing" ng-options="thing for thing in sizes.things"></select>
    

    You can see a working example here.

    PS It was actually not working for either the array or the object. Adding the ng-model attribute gets both working.