Kyle
Kyle

Reputation: 1183

ng-options not working as expected

I'm having an issue trying to loop through an array of objects using ng-options.

html

<select class="select-width" ng-options="type as type.name for type in types">

js

$scope.types = [{name: "Advanced Yield Analysis"}, 
            {name: "Yield by Hybrid"},
            {name: "Yield by planting data"},
            {name: "Yield by soil type"},
            {name: "Yield by management zone"},
            {name: "Yield by population/seeding rate"},
            {name: "Yield by Treatment"},
            {name: "Total Yield by Grower / Location / MC"}];

Here is a JS-fiddle.

Upvotes: 0

Views: 47

Answers (1)

Vineet
Vineet

Reputation: 4635

You only need to pass the ng-model. Ng-options will not work when you're not assigning a model.

By default, ngModel watches the model by reference, not value. This is important when binding any input directive to a model that is an object or a collection.

try

<select class="select-width" ng-model="demo" ng-options="type as type.name for type in types"></select> 

Fiddle

Upvotes: 1

Related Questions