1

I have a drop down with angular model binding and data generated from server side. I wish to pre-select the first element of data, however angular js generate an unknown options above my drop down options and I couldn't get my option selected.

Source:

<select ng-model="currency" >
    {% for currency in currencies %}
    <option {% "selected='selected'" if(index==1) %} value="{{ currency["code"]}}">{{ currency["name"]}}</option>
    {% endfor %}
</select>

Generated Data

<select ng-model="currency">
    <option value="? object:004 ?"></option>
    <option value="EUR">Euro</option>
    <option value="SEK">Svenske kroner</option>
    <option value="GBP">Engelske Pund</option>
    <option value="DKK">Danske kroner</option>
    <option  value="USD">Amerikanske Dollar</option>
    <option value="NOK">Norske kroner</option>
</select>

Any idea how to remove the extra option field?

3 Answers 3

3

Please try this:

<select ng-model="currency" >
    <option ng-repeat="currency in currencies" ng-selected="$first" ng-value="currency.code">{{ currency.name }}</option>
</select>
Sign up to request clarification or add additional context in comments.

Comments

2

You can do like below. you need to specify which currency you wish to get selected.

<select ng-model="currency" >
    <option ng-repeat="currency in currencies" ng-selected="currency.code == 'USD' ? true:false" ng-value="currency.code">{{ currency.name }}</option>
</select>

Comments

1

Try this in your controller

$scope.currency = $scope.currencies[0].code;

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.