1

I have a dropdown and its being populated correctly by three recordes d1, d2, d3 and their values are 0,1,2

<div class="form-group">
                    <label for="Distance" class="col-md-3">
                        Distance :
                    </label>
                    <div class="col-md-4">
                        <select id="Distance" name="Distance" class="form-control selectwidthauto" ng-model="ModelData.Distance" 
                    ng-options="distance.Code as distance.Description for distance in DropdownData.Distances">
                        </select>
                    </div>
                </div>

Why the dropdown selection is not working whereas I am getting the value of {{ModelData.Distance}} as 2 on the html.

1 Answer 1

1

You can use indexOf to check the iteration number. This will help you to identify the specific iteration that is selected.

<div ng-app ng-controller="app">
    <select 
          ng-model="item"
          ng-options="values.indexOf(item) as item for item in values"></select>
    item: {{item}}
</div>

function app($scope) {
    $scope.values = ["Value1","Value2"];
    $scope.item= 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is there any specific reason to use indexOf? Its working for the other dropdowns on the page.
No. Not any specific reason. But since there is no way to note iteration in ng-options like we can use $index in ng-repeat so it provides a handy tool. For your case you have to post your full code for detailed analysis. You can give plunker link for your code.
ok. but I can not use indexOf as my model is containing ID not the value.
Your code looks fine to me but I don't know how it is binding on controller side. So can you give full code or place your code on JSfiddle or Plunker to analyze?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.