ng-options JSON的默认值

时间:2015-12-07 16:38:04

标签: angularjs html5

我一直在使用模态表单,表单非常适合提交数据,但现在我需要表单来编辑数据。所以我想从服务器的回调中填充表单。表格的结构如下:

HTML:

<div class="form-group modalContent">
   <label for="manufacturer">MANUFACTURER</label>
   <select class="form-control" id="manufacturersList"
      ng-model="form.manufacturers" required
      ng-options="man.manufacturer for man in manufacturers | orderBy:'manufacturer'"></select>
</div>

AngularJS:

$scope.form = {
   //The value will be the value from the callback
   "manufacturers": "Acer"
}

编辑:回电

    {"status":{"error":"none","code":200,"description":"none","message":"success"},"manufacturers":[{"manID":"2",
"manufacturer":"Acer"},{"manID":"3","manufacturer":"Honeywell"},{"manID":"11","manufacturer":"HP"},{"manID":"9","manufacturer":"Juniper"},{"manID":"1","manufacturer":"Lenovo"},{"manID":"6","manufacturer":"Logitech"},{"manID":"8","manufacturer":"Netgear"},{"manID":"7","manufacturer":"Other"},{"manID":"10","manufacturer":"StarTech"},{"manID":"5","manufacturer":"Viewsonic"},{"manID":"4","manufacturer":"Zebra"}]}

这不应该填充<SELECT>中的值吗?

1 个答案:

答案 0 :(得分:1)

ng-model指向包含所选值的变量。 根据您在ng-options中使用的语法,您应该manufactures直接放置$scope

<select class="form-control" id="manufacturersList"
  ng-model="selectedManufacturer" required
  ng-options="man for man in manufacturers | orderBy:'manufacturer'">
</select>

$scope.manufacturers = ["Acer", 'HP', 'Toshiba', 'Lenovo', 'ASUS'];

Plunker

中的示例

希望这有帮助