0

I have a key field that should be selected into a SELECT html element. This SELECT is populated with an ng-repeat.

I define two SELECTs, one with ng-repeat, other with ng-options:

<div ng-controller="MyDisplayCtrl">
    <select ng-model="context.id1">
        <option value="">-</option>
        <option ng-repeat="i in lista" value="{{i.id}}">{{i.value}}</option>
    </select> Should have "three" selected, but shows - <br/>

    <select ng-model="context.id2" ng-options="i.value for i in lista">
    </select> Selects "three" automatically <br/>
</div>

And my controller goes like this:

function MyDisplayCtrl($scope) {
    var x1 = {id: 1, value: 'one'};
    var x2 = {id: 2, value: 'two'};
    var x3 = {id: 3, value: 'three'};
    window.setTimeout(function() {
        $scope.$apply(function() {
            $scope.lista = [x1, x2, x3];
        });
    }, 2000);

    $scope.context = {id1: 3, id2: x3};
}

I'd have this list brought to client by an AJAX, so I used a setTimeout() here.

The approach with ng-options works, but I wouldn't have an object, sole its key.

Take a look at this fiddle: http://jsfiddle.net/nCG2H/

Problem: I'm trying to understand why the first SELECT doesn't work, algo because I got it to work depending on timing from AJAX response.

1 Answer 1

2

Please use ng-selected="{{i.id}}" instead of value="{{i.id}}"...

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.