ng-option正在替换我的默认选项

时间:2018-05-23 18:43:04

标签: javascript angularjs angular1.6

我有一个选择标记为

 <select ng-model="vm.fooModel"
         ng-options="item.id as item.name for item in vm.fooOptions" >
     <option value="none">Select</option>
 </select>

问题在于,当渲染角度1.6时,删除我的默认选项并添加空选项

     <option value="?" selected="selected"></option>

“vm.fooModel”的初始模型值为“none”。 ̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶

3 个答案:

答案 0 :(得分:0)

ng-repeat选项使用<select>方法:

&#13;
&#13;
angular.module("app",[])
.controller("ctrl", function(){
  var vm = this;
  vm.fooModel="none";
  vm.fooOptions=[
    {id: 1, name:"one"},
    {id: 2, name:"two"},
  ];
})
&#13;
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app" ng-controller="ctrl as vm">
    <select ng-model="vm.fooModel">
      <option value="none" disabled>Select</option>
      <option ng-repeat="item in vm.fooOptions" ng-value="item.id">
        {{item.name}}
      </option>   
    </select>
    value={{vm.fooModel}}
 </body>
&#13;
&#13;
&#13;

有关详细信息,请参阅AngularJS <select> Directive API Reference - Using ngRepeat to generate select options

答案 1 :(得分:0)

为控制器中的选项数组添加默认选项。

Promise.all
angular.module("app", [])
  .controller("ctrl", function() {
    var vm = this;
    vm.fooModel = "none";
    vm.fooOptions = [{
        id: "none",
        name: "default"
      },
      {
        id: 1,
        name: "one"
      },
      {
        id: 2,
        name: "two"
      },
    ];
  })

答案 2 :(得分:0)

<option ng-repeat="item in vm.fooOptions track by item.id" ng-value="item.id">

在ng-repeat

中使用track