0

I have a set of options in my controller that looks like this:

$scope.options = [
    {one: 'ONE'},
    {two: 'TWO'},
    {three: 'THREE'}
];

My view looks like the following currently looks like this:

<div ng-repeat="goal in objectives">
   ...
  <select ng-model="goal.choices" ng-options="value for (key, value) in options">  </select>
   ...
</div>

PROBLEM: the resulting dropdown is not sorted by obj occurence in array rather by alpha of each objects key AND there is no default option selected, i want the dropdown to default to 'one' not ''

What is the ng-options expression need to make this work?????

Thanks

1 Answer 1

1

Your $scope.options array isn't usable in ngOptions because you have an array of three entirely different objects (one has a one property, another a two property, and the last a three property). If you want the select to default to $scope.choices[0], then goal.choices needs to be set to $scope.options[0].

I had to make some guesses here, because you didn't include what $scope.objectives was, but I can imagine you were going for something like this:

http://jsfiddle.net/A5KkM/

HTML

<div ng-app ng-controller="x">
    <div ng-repeat="goal in objectives">{{goal.choice}}
        <select ng-model="goal.choice" ng-options="o.name for o in options"></select>
    </div>
</div>

JavaScript

function x($scope) {
    $scope.options = [{
        name: 'ONE'
    }, {
        name: 'TWO'
    }, {
        name: 'THREE'
    }];

    $scope.objectives = [{ choice: $scope.options[0] }, { choice: $scope.options[1] }];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.