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

iCo*_*ode 76 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>
Run Code Online (Sandbox Code Playgroud)

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;
});
Run Code Online (Sandbox Code Playgroud)

但它似乎没有用.我怎样才能解决这个问题?这是JSFiddle演示

Shi*_*abu 94

供参考:为什么angularjs在select中包含一个空选项

optionng-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;
});
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

正在使用JSFiddle演示

更新(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>
Run Code Online (Sandbox Code Playgroud)

而在JS中无需初始化价值.

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

  • 为什么需要将`$ scope`从`$ scope`移动到`$ scope.feed`? (3认同)
  • 解释真的会有所帮助 (2认同)

小智 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>
Run Code Online (Sandbox Code Playgroud)

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

  • +1表示唯一没有将模型初始化为列表中第一个选项的答案.我奇怪的实现需要选择列表开始空白,但在单击选择列表时不包括空白选项.这是唯一对我有用的解决方案.谢谢! (4认同)

小智 21

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

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

$scope.feed.config = $scope.configs[0];
Run Code Online (Sandbox Code Playgroud)

并将您的选择更新为如下所示:

<select ng-model="feed.config" ng-options="item.name for item in configs">
Run Code Online (Sandbox Code Playgroud)


Roy*_*M J 14

另一种解决方法是利用:$firstinng-repeat

用法:

<select ng-model="feed.config">
    <option ng-repeat="template in configs" ng-selected="$first">{{template.name}}</option>
</select>
Run Code Online (Sandbox Code Playgroud)

参考文献:

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

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

干杯


Bha*_*a K 5

有多种方法,例如 -

<select ng-init="feed.config = options[0]" ng-model="feed.config"
        ng-options="template.value as template.name for template in feed.configs">
</select>
Run Code Online (Sandbox Code Playgroud)

或者

$scope.feed.config = $scope.configs[0].name;
Run Code Online (Sandbox Code Playgroud)