简体   繁体   English

如何将第一个选择选项设置为始终为空白

[英]How to set the first select option to always be blank

I am using angularjs in a project and in which I am using ng-options for generating <select>.我在一个项目中使用 angularjs,并在其中使用 ng-options 生成 <select>。

Initially, when the pages reload and no option element is selected, the HTML generated like below:最初,当页面重新加载并且没有选择选项元素时,生成的 HTML 如下所示:

<select size="3" ng-model="item" ng-options="s.name for s in itemlist">
<option value="?" selected="selected"></option>
<option value="0">Item 1</option>
<option value="1">Item 2</option>
<option value="2">Item 3</option>
</select>

But when I select an element (ex. Item 2) the first blank select is gone.但是当我选择一个元素(例如第 2 项)时,第一个空白选择消失了。 I know its happening as ng-model is being set by select value.我知道它的发生是因为 ng-model 是由选择值设置的。 But I want first select always blank so that user can reset the filter.但我想首先选择始终空白,以便用户可以重置过滤器。

This will do the work for you:这将为您完成工作:

<select size="3" ng-model="item" ng-options="s.name for s in itemlist">
    <option value="">Select Option</option>
</select>

For anyone out there that treat "null" as valid value for one of the options (so imagine that "null" is a value of one of the items in typeOptions in example below), I found that simplest way to make sure that automatically added option is hidden is to use ng-if.对于将“null”视为选项之一的有效值的任何人(所以想象“null”是下面示例中 typeOptions 中的一项的值),我发现了一种最简单的方法来确保自动添加隐藏选项是使用 ng-if。

<select ng-options="option.value as option.name for option in typeOptions">
    <option value="" ng-if="false"></option>
</select>

Why ng-if and not ng-hide?为什么是 ng-if 而不是 ng-hide? Because you want css selectors that would target first option inside above select to target "real" option, not the one that's hidden.因为您想要将上面的第一个选项作为目标的 css 选择器选择目标“真实”选项,而不是隐藏的选项。 It gets useful when you're using protractor for e2e testing and (for whatever reason) you use by.css() to target select options.当您使用量角器进行 e2e 测试和(无论出于何种原因)使用 by.css() 来定位选择选项时,它会变得很有用。

Kevin - Sưu Tầm凯文 - Sưu Tầm

You can forgo using the ng-options and use ng-repeat instead and make the first option blank.您可以放弃使用ng-options并改用ng-repeat并将第一个选项设为空白。 See example below.请参见下面的示例。

<select size="3" ng-model="item">
    <option value="?" selected="selected"></option>
    <option ng-repeat="s in itemlist" value="{{s.value}}">{{s.name}}</option>
</select>

看来您最好的选择是将空白项目作为项目列表中的第一项包含在内。

The solution above will corrupt the model with junk data.上面的解决方案将使用垃圾数据破坏模型。 Please use the directive to reset the model in the right way JS: Select Option请使用指令以正确的方式重置模型 JS:选择选项

Directive"
 .directive('dropDown', function($filter) {
    return {

        require: 'ngModel',
        priority: 100,
        link: function(scope, element, attr, ngModel) {

            function parse(value) {

                if (value) {

                    if(value === "")
                    {
                        return "crap"
                    }
                    else
                    {
                        return value;
                    }

                } else {
                    return;
                }
            }


            ngModel.$parsers.push(parse);

        }
    };
});

I had same problem and after a lot of googling,I understood the main issue was related to my angular version, if you use Angular 1.6.x , just use the ng-value directive and it will work as expected.我遇到了同样的问题,经过大量谷歌搜索,我了解到主要问题与我的 Angular 版本有关,如果您使用 Angular 1.6.x ,只需使用ng-value指令,它就会按预期工作。

also I set an initial value in my controller, like this :我还在控制器中设置了一个初始值,如下所示:

$scope.item = 0;
        <select ng-model="dataItem.platform_id"
            ng-options="item.id as item.value for item in platformList"
            ng-init="dataItem.platform_id=dataItem.platform_id||platformList[0].id"></select>

Controller控制器

$scope.item = '';

$scope.itemlist = {
   '' = '',
   'Item 0' = 1,
   'Item 1' = 2,
   'Item 2' = 3
};

HTML HTML

<select data-ng-model="item" data-ng-options="v as k for (k, v) in itemlist"></select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM