title: How to pre-select a select option in an Angularjs select that's filled with ng-options
body: <p>I have the following select that is filled with all the countries in the world, I have an Angularjs controller that calls a method that returns me the <code>idCountry</code> of one of those countries that variable <code>idCountry</code> is the id of the object and not the index of the position that object takes in the select.  </p>
  
  <p>I need to pre-select that country in the select using the <code>idCountry</code>, is there a way to get the index that this object has in the select using the id of the object, I read some articles saying that this can be done using <code>track by</code> in the ng-options expression but I don't know how.</p>
  
  <p>If I manage to get the index I could pre-select the option like this </p>
  
  <pre><code>$scope.countriesSelect = $scope.countriesSelect[put here the select index that I can find]
  </code></pre>
  
  <p>Here is my select </p>
  
  <pre><code>&lt;select name="selectCountries" class="form-control" 
                                          id="selectCountries" 
                                          ng-model="selectCountries"
                                          ng-options="country.name for country in countryList track by country.idCountry" 
                                          ng-change=""
  required&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>and here is what I was trying to do in my Controller</p>
  
  <pre><code>var idCountry = id;
  $scope.selectCountries= $scope.selectCountries[idCountry]
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I edited my code,  I have read similar questions but most of the questions are about how to initialize a select with a default value, I'm trying to do an edit page where the user can check all the values that he put during his registration, and he can edit them so, this select needs to be initialized with the value that he selected.</p>
  
  <p>I can get the id of any object of the list of objects </p>
  
  <p>here is the list of objects that populates my select </p>
  
  <pre><code>[{idCountry: 2, name: countryName}, {idCountry: 4, name: AnothercountryName}, {idCountry: 7, name: rockyCOuntry}]
  </code></pre>
  
  <p>I have a method that returns me the <code>idCountry</code> and I want to know if there's a way to get the index of the select using this <code>idCountry</code> 
  or by putting some expression in my <code>ng-options</code> expression like using <code>track by</code> or something.</p>
  
  <p>I tried this <code>$scope.selectCountries= $scope.selectCountries[idCountry]</code>
  but this doesn't return me the correct country if I put <code>7</code> instead of setting the select in rockyCOuntry it sets the select in another country, I believe that is because the id of the list object doesn't correspond with the same index of the select that occupies that object.</p>