title: Selecting default value using ngModel ngRepeat, ngOptions
body: <p>Hopefully someone can help.</p>
  
  <p>I am developing an application using HTML AngularJs which uses ng-repeat,ng-options and ng-model and populates multiple rows based on the data in the database for a user. Each row has static data coming from DB (returned as object via restAPI) and dynamic select option for user selection. Select option is hardcoded in app.js and linked to model on HTML for DB update upon selection using update button. Each row has its own button and i can see update function is working at row level.</p>
  
  <p>I want to set the default value of the drop down list dynamically as value of an element coming from database. Object is same as one being used to populate rows with static data . </p>
  
  <p>Code is in the fiddle at <a href="https://jsfiddle.net/6j88L61y/4/" rel="nofollow noreferrer">https://jsfiddle.net/6j88L61y/4/</a></p>
  
  <p>HTML below</p>
  
  <pre><code>&lt;body&gt;
  &lt;h1 align="center"&gt;User Tracker&lt;/h1&gt;
  &lt;div ng-controller="MainController as main"&gt;
  &lt;div&gt;
  &lt;p&gt;Please Enter ID &lt;/p&gt;
  &lt;p&gt;
  &lt;input type="text" ng-model="main.model.userName"&gt;&lt;/input&gt;
  &lt;button ng-click="main.model.getOrgList()"&gt;List State List&lt;/button&gt;
  &lt;/p&gt;
  &lt;/div&gt;
  &lt;hr ng-show="main.model.formSubmitted"&gt;&lt;/hr&gt;
  &lt;div&gt;
  &lt;table class="table table-bordered" border="1"&gt;
  &lt;thead&gt;
  &lt;tr&gt;
  &lt;th&gt;ID&lt;/th&gt;
  &lt;th&gt;User Name&lt;/th&gt;
  &lt;th&gt;Action&lt;/th&gt;
  &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
  &lt;tr ng-repeat="org in main.model.orgList" id="{{org.id}}"&gt;
  &lt;td&gt;{{org.id}}&lt;/td&gt;
  &lt;td align="center"&gt;{{org.user}}&lt;/td&gt;
  &lt;td align="center"&gt;
  &lt;select ng-model="main.model.selectedRecord.appCurrentStateSelected[$index]" ng-options="option.value  as option.value for option in main.model.appCurrentStateList" &gt;&lt;/select&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;button ng-click="main.model.updateAppDetailsList({id:org.id,userName:org.name,appCrntState:main.model.selectedRecord.appCurrentStateSelected})"&gt;Update&lt;/button&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>JS</p>
  
  <pre><code>"use strict";
  
  angular.module('myApp',[]);
  
  angular.module('myApp').service('AppModel', function( $http) {
  this.userId='';
  this.userName ="";
  this.formSubmitted="";
  this. selectedRecord ={appCurrentStateSelected:''};
  this.appCurrentStateList =[{name: 'New',value:'New',id:1}, {name: 'InUse',value:'InUse',id:2},{name: 'Inactive',value:'Inactive',id:3},{name: 'Deleted',value:'Deleted',id:4}];     
  this.submittedAppDetailsList=[];
  console.log(' json sample:'+this.submittedAppDetailsList);  
  var path = 'home';
  var currentProtocol = location.protocol;
  var host =location.host;
  var apiHost = currentProtocol+'//'+host+'/api/';
  console.log('API url is : ' +apiHost);
  
  // Get method
  
  this.getOrgList = function() {
  
  var path = 'home/userid';
  console.log(this.userName);
  console.log(this.selectedRecord);   
  $http.get(apiHost+path+'/'+this.userName+'/')
  .then(function(response) {
  this.orgList =response.data;
  this.formSubmitted = true;      
  console.log(response.data);
  }.bind(this),
  function(response) {
  console.log(response.data);
  });
  }
  
  // Post method
  
  this.updateAppDetailsList = function(appdetailsList) {
  
  var path = 'home/update';
  
  console.log(this.selectedRecord);   
  $http.post(apiHost+'home/update/',appdetailsList)
  .then(function(response) {
  this.submittedAppDetailsList.push(response.data);
  this.formSubmitted = false;     
  console.log(response.data);
  }.bind(this),
  function(response) {
  console.log('Error : '+response.data);
  });
  }   
  
  });
  
  
  angular.module('myApp').controller('MainController', ['AppModel', function (AppModel) {
  
  this.model = AppModel;
  
  
  }]);
  </code></pre>