0

Angular version - 1.2.28 (Legacy, pending upgrade)

I'm trying to set the selected value for my select element via the model value.

In my Controller I have the below line;

$scope.customer.country = 'GB';

And in my view I have the below select element;

<select
    ng-model="customer.country"
    ng-options="country as country.name for country in countries track by country.alpha2">
</select>

I was expecting, that should I have an object in countries with the alpha2 value of 'GB', it would be selected by default on render.

However the actual behaviour is the empty option is selected by default.

How do I set the default selected value via the model (ng-model) value?

1
  • I thought you have rebind the dropdown after initialize the object? Commented May 4, 2017 at 10:35

1 Answer 1

2

It seems initialization is wrong

$scope.customer.country = 'GB';

You have to initialize with the same object of array which is binded (countries)

should look like

$scope.customer.country = $scope.countries[0];

CodePen

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.