使用AngularJS从选择选项中删除空白选项

时间:2013-12-23 07:33:05

标签: angularjs angularjs-select

我是AngularJS的新手。我搜索了很多,但它并没有解决我的问题。

我第一次在选择框中看到一个空白选项。

这是我的HTML代码

<div ng-app="MyApp1">
    <div ng-controller="MyController">
        <input type="text" ng-model="feed.name" placeholder="Name" />
        <select ng-model="feed.config">
            <option ng-repeat="template in configs">{{template.name}}</option>
        </select>
    </div>
</div>

JS

var MyApp=angular.module('MyApp1',[])
MyApp.controller('MyController', function($scope) {
    $scope.feed = {};

      //Configuration
      $scope.configs = [
                                 {'name': 'Config 1',
                                  'value': 'config1'},
                                 {'name': 'Config 2',
                                  'value': 'config2'},
                                 {'name': 'Config 3',
                                  'value': 'config3'}
                               ];
      //Setting first option as selected in configuration select
      $scope.feed.config = $scope.configs[0].value;
});

但它似乎不起作用。 我怎样才能解决这个问题?这是JSFiddle Demo

12 个答案:

答案 0 :(得分:90)

供参考:Why does angularjs include an empty option in select

  

当传递给option的一组选项中不存在ng-model引用的值时,会生成空ng-options。这可以防止意外的模型选择:AngularJS可以看到初始模型在选项集中未定义或不定义,并且不希望自己决定模型值。

     

简而言之:空选项意味着没有选择有效模型(有效我的意思是:来自选项集)。您需要选择一个有效的模型值来摆脱这个空选项。

像这样更改你的代码

var MyApp=angular.module('MyApp1',[])
MyApp.controller('MyController', function($scope) {
  $scope.feed = {};

  //Configuration
  $scope.feed.configs = [
    {'name': 'Config 1',
     'value': 'config1'},
    {'name': 'Config 2',
     'value': 'config2'},
    {'name': 'Config 3',
     'value': 'config3'}
  ];
  //Setting first option as selected in configuration select
  $scope.feed.config = $scope.feed.configs[0].value;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app="MyApp1">
  <div ng-controller="MyController">
    <input type="text" ng-model="feed.name" placeholder="Name" />
    <!-- <select ng-model="feed.config">
<option ng-repeat="template in configs">{{template.name}}</option>
</select> -->
    <select ng-model="feed.config" ng-options="template.value as template.name for template in feed.configs">
    </select>
  </div>
</div>

Working JSFiddle Demo

更新(2015年12月31日)

如果您不想设置默认值并想要删除空白选项,

<select ng-model="feed.config" ng-options="template.value as template.name for template in feed.configs">
      <option value="" selected="selected">Choose</option>
</select>

在JS中无需初始化值。

$scope.feed.config = $scope.feed.configs[0].value;

答案 1 :(得分:52)

虽然上面的所有建议都有效,但有时候我需要在最初进入我的屏幕时有一个空的选择(显示占位符文本)。因此,为了防止选择框在我的列表的开头(或有时在结尾)添加这个空选择,我使用这个技巧:

HTML代码

<div ng-app="MyApp1">
    <div ng-controller="MyController">
        <input type="text" ng-model="feed.name" placeholder="Name" />
        <select ng-model="feed.config" ng-options="template.value as template.name for template in feed.configs" placeholder="Config">
            <option value="" selected hidden />
        </select>
    </div>
</div>

现在你已经定义了这个空选项,所以选择框很开心,但你保持隐藏。

答案 2 :(得分:21)

这是一个更新的小提琴:http://jsfiddle.net/UKySp/

您需要将初始模型值设置为实际对象:

$scope.feed.config = $scope.configs[0];

并将您的选择更新为:

<select ng-model="feed.config" ng-options="item.name for item in configs">

答案 3 :(得分:13)

要解决的另一种方法是在$first

中使用: ng-repeat

用法:

<select ng-model="feed.config">
    <option ng-repeat="template in configs" ng-selected="$first">{{template.name}}</option>
</select>

<强>参考

ngSelected https://docs.angularjs.org/api/ng/directive/ngSelected

$ first https://docs.angularjs.org/api/ng/directive/ngRepeat

干杯

答案 4 :(得分:3)

有多种方式,如 -

<select ng-init="feed.config = options[0]" ng-model="feed.config"
        ng-options="template.value as template.name for template in feed.configs">
</select>

或者

$scope.feed.config = $scope.configs[0].name;

答案 5 :(得分:3)

这是一种解决方法,但就像一个魅力。只需添加<option value="" style="display: none"></option>作为填充。喜欢在:

<select size="4" ng-model="feed.config" ng-options="template.value as template.name for template in feed.configs">
       <option value="" style="display: none"></option>
</select>

答案 6 :(得分:2)

像这样更改您的代码。 你忘记了选项里面的价值。 在分配ng-model之前。它必须有价值。只有这样它才能获得未定义的值。

<div ng-app="MyApp1">
<div ng-controller="MyController">
    <input type="text" ng-model="feed.name" placeholder="Name" />
     <select ng-model="feed">
        <option ng-repeat="template in configs" value="template.value">{{template.name}}    
 </option>
    </select>
    </div>
 </div>

JS

 var MyApp=angular.module('MyApp1',[])
 MyApp.controller('MyController',function($scope) {  
      $scope.feed = 'config1';      
      $scope.configs = [
         {'name': 'Config 1', 'value': 'config1'},
         {'name': 'Config 2', 'value': 'config2'},
         {'name': 'Config 3', 'value': 'config3'}
      ];
 });

答案 7 :(得分:2)

如果您只是想删除空格,请使用ng-show,然后就完成了!无需在JS中初始化值。

<select ng-model="SelectedStatus" ng-options="x.caption for x in statusList">
    <option value="" ng-show="false"></option>
</select>`

答案 8 :(得分:1)

使用 ng-value 代替 value

$scope.allRecs = [{"text":"hello"},{"text":"bye"}];
$scope.X = 0;

,然后在html文件中应类似于:

<select ng-model="X">  
     <option ng-repeat="oneRec in allRecs track by $index" ng-value="$index">{{oneRec.text}}</option>
</select>

答案 9 :(得分:0)

对我来说,这个问题的答案是使用@RedSparkle提出的<option value="" selected hidden />,再加上ng-if="false"在IE中工作。

因此,我的完整选择是(与我之前写的内容有所不同,但这并不重要,因为ng-if)

<option value="" ng-if="false" disabled hidden></option>

答案 10 :(得分:0)

最后它对我有用。

<select ng-init="mybasketModel = basket[0]" ng-model="mybasketModel">
        <option ng-repeat="item in basket" ng-selected="$first">{{item}}</option>
</select>


答案 11 :(得分:-1)

同时检查您是否有任何假值。 如果你确实有假值,Angularjs会插入一个空选项。 由于价值不重,我挣扎了2天。 我希望它会对某人有所帮助。

我有[0,1]的选项,最初我将模型设置为0,因为它插入了空选项。 解决方法是[“0”,“1”]。

相关问题