Angular下拉默认值不起作用

时间:2014-06-04 08:25:01

标签: angularjs

在我的角度应用程序中,我创建了一个包含sim名称的下拉列表。这些名称来自API。我还将默认值设置为All sim。但它没有用。这是我的代码段。

<select ng-model="selectSim" class="selectpicker" ng-options=" item.description for item in simList">
<option value="all">Select All</option>

当我渲染此页面时,我可以看到包含Sim名称的下拉列表,但我的“全选”选项丢失了。

我无法弄清楚我的代码中是不是错了。

2 个答案:

答案 0 :(得分:2)

设置option value = ""

<select ng-model="selectSim" class="selectpicker" ng-options=" item.description for item in simList">
      <option value="">Select All</option>
</select>

单个硬编码元素(值设置为空字符串)可以嵌套到元素中。然后,此元素将表示null或&#34;未选择&#34;选项。

答案 1 :(得分:0)

试试这个

这是html代码(不需要option代码)

<select ng-model="selectSim" class="selectpicker" ng-options=" item.description for item in simList">

并在您的控制器代码中以这种方式编写,其中将值分配给simList

$scope.simList.push({ description : "Select All", Id: 0 }); // this line helps to dynamoically add defualt value in `$scope.simList` array, now the default item added in bottom side 

  $scope.selectSim= $scope.simList[$scope.simList.length - 1];// this is helps to assign default item `selectSim` model 
  

'*'此处Id: 0是您的值名称和虚拟值。您需要更改值名称

相关问题