Search code examples
angularjsng-options

Select keeps resetting even though model updates


Here's the html for a select I'm trying to build.

<select
  class="form-control"
  ng-options="country.code as country.name for country in countries track by country.code"
  ng-model="country_code"
  name="country_code">
</select>

The select populates, and when I chose an option the model does update. However, the select keeps resetting and I'm not sure why.

Here's a plunker: http://plnkr.co/edit/nEKP0xDhrdrIeDPml7Am?p=preview

The reason I need track by is that I'm building a form that will be submitted the old-fashioned non-ajax way.


Solution

  • In that case you are better of using ng-repeat with options element

    http://plnkr.co/edit/yYysAHs6Ks5cCY4ZMjyg?p=preview

    <select
          class="form-control"
          ng-model="country_code"
          name="country_code">
          <option value="{{country.code}}" ng-repeat="country in countries track by country.code">{{country.name}}</option>
    </select>
            {{country_code}}