【问题标题】:Remove Blank option from Select Option with AngularJS使用 AngularJS 从选择选项中删除空白选项
【发布时间】:2013-12-23 07:33:04
【问题描述】:

我是 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

【问题讨论】:

    标签: angularjs angularjs-select


    【解决方案1】:

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

    ng-model 引用的值在传递给ng-options 的一组选项中不存在时,将生成空的option。这样做是为了防止意外选择模型: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;

    【讨论】:

    • 解释一下会很有帮助
    • 为什么需要将configs$scope 移动到$scope.feed
    • 那么......当我确实有一个有效值并且它仍然给我一个空行而不是使用该选项时,这意味着什么?模型 - 0,选项 0、50、100 - 选择从空白行开始
    【解决方案2】:

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

    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>
    

    现在你已经定义了这个空选项,所以选择框很高兴,但你把它隐藏了。

    【讨论】:

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

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

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

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

    并更新您的选择,使其看起来像这样:

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

    【讨论】:

      【解决方案4】:

      另一种解决方法是利用:$first in 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

      $firsthttps://docs.angularjs.org/api/ng/directive/ngRepeat

      干杯

      【讨论】:

        【解决方案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>
        

        或者

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

        【讨论】:

          【解决方案6】:

          这是一种解决方法,但效果很好。只需添加&lt;option value="" style="display: none"&gt;&lt;/option&gt; 作为填充符。喜欢在:

          <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>
          

          【讨论】:

          • 我猜这不会隐藏 IE 中的默认选项
          • 没有在 IE 中专门测试过
          【解决方案7】:

          如果您只想删除空格,请使用 ng-show 即可!无需在 JS 中初始化值。

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

          【讨论】:

            【解决方案8】:

            像这样更改您的代码。 您忘记了选项内部的值。 在分配 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'}
                  ];
             });
            

            【讨论】:

              【解决方案9】:

              使用 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>
              

              【讨论】:

                【解决方案10】:

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

                所以我的完整选项是(与我之前写的有所不同,但这并不重要,因为 ng-if):

                &lt;option value="" ng-if="false" disabled hidden&gt;&lt;/option&gt;

                【讨论】:

                  【解决方案11】:

                  终于对我有用了。

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

                  【讨论】:

                    【解决方案12】:

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

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

                    【讨论】:

                      猜你喜欢
                      • 2017-07-31
                      • 2014-07-24
                      • 2021-01-20
                      • 2014-07-15
                      • 2021-05-24
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2010-10-18
                      相关资源
                      最近更新 更多