1

I'm working on this fiddle HERE

this is my controller

angular.module('demoApp', []).controller('DemoController', function($scope) {

  $scope.options = [
    { label: 'one', value: 'a' },
    { label: 'two', value: 'b' },
    { label: 'three', value: 'c' },
    { label: 'four', value: 'd' }
  ];

  $scope.selected = [{ 'sel': 'a' },{ 'sel': 'b' },{ 'sel': 'c' } ] ;


});

the $scope.options is the option for my select dom and the $scope.selected is the selected item in my select dom

this is my index.html

<body ng-app="demoApp">
    <div ng-controller="DemoController">
        <div ng-repeat="data in selected">
            <select ng-model="data.sel"
                ng-options="opt as opt.label for opt in options">
            </select>
            selected must be : {{data.sel}}
        </div>
    </div>
</body>

what I had is this

enter image description here

what I need is on first load the selected must be selected like this

enter image description here

could anyone help me?

2 Answers 2

2

You can use ngInit for default:

<body ng-app="demoApp">
    <div ng-controller="DemoController">
        <div ng-repeat="data in selected">
            <select ng-model="data.sel"
            ng-options="opt.value as opt.label for opt in options" ng-init="data.sel = data.sel || options[$index].value">
        </select>
            selected must be : {{data.sel}}
        </div>
    </div>
</body>

fiddle

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

3 Comments

hi I try to change the selected object to $scope.selected = [{ 'sel': 'a' },{ 'sel': 'a' },{ 'sel': 'a' } ] ; it seem not working could you try this fiddle jsfiddle.net/kodewrecker/qWzTb/1713
Did you try above answer in comment?
The original fiddle in the answer has also been updated
0

You could use ng-init to select the ng-model value by their $index:

<select ng-model="data.sel" ng-options="opt as opt.label for opt in options" ng-init="data.sel = options[$index]"></select>

Updated your Fiddle.

3 Comments

hi I try to change the selected object to $scope.selected = [{ 'sel': 'a' },{ 'sel': 'a' },{ 'sel': 'a' } ] ; it seem not working could you try this fiddle jsfiddle.net/kodewrecker/qWzTb/1713
I don't get what your are trying to do.
the $scope.selected must be the selected value of select dom sir. since the ng-selected listen to index but $scope.selected returns value not index.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.