即使设置了默认值

时间:2017-02-25 08:04:57

标签: angularjs drop-down-menu

我已经拥有了我用该特定选择框参加的模型的现有值,并且我有不同的源列表来渲染选项,在这种情况下,我无法在最初选择具有默认值的选择框。

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 这是针对同一问题的demo plnkr链接。

如果有任何想法,请把我带出来。

1 个答案:

答案 0 :(得分:1)

避免在选择中使用ng-repeat。正确的用法是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>

这是您更新的plunkr