0

I already have existing value of the model I atteched with that particular select box, and I have different source list to render options, in that case I could not get select box to be selected with default value initially.

HTML

<div ng-controller="SampleCtrl as vm">
  <input type="text" ng-model="vm.new_object.name"><br/>
  <select name="test" id="test" ng-model="vm.new_object.provider_id">
    <option ng-repeat="opt in vm.list" value="{{opt.id}}" ng-selected="vm.new_object.provider_id == opt.id">
      {{opt.name}}
    </option>
  </select>
</div>

SCRIPT

angular.module('app', [])

.controller('SampleCtrl', function($scope) {

vm = this;


vm.list = [{
  "id":1
  ,"name":"sdfgg"
  ,"website":"dfgdg"
  ,"is_active":"1"
  ,"is_deleted":"1"
}, {
  "id":2
  ,"name":"sfd"
  ,"website":"sdfsdf"
  ,"is_active":"1"
  ,"is_deleted":"0"
}, {
  "id":3
  ,"name":"sfs"
  ,"website":"dfsdfs"
  ,"is_active":"0"
  ,"is_deleted":"0"
}];

vm.new_object = {
  "name": "some thing"
  ,"provider_id": 2
}

})

Link this is demo plnkr link for the same issue.

Please haelp me out if have any idea arround it.

1 Answers1

1

avoid using ng-repeat in selects. The proper usage is ng-options

<select name="test" id="test" ng-model="vm.new_object.provider_id"
         ng-options="opt.id as opt.name for opt in vm.list">
</select>

Here is your updated plunkr

Tah
  • 1,526
  • 14
  • 22