ng-options how to set first select always blank

JavascriptAngularjsOptions

Javascript Problem Overview


I am using angularjs in a project and in which I am using ng-options for generating

Javascript Solutions


Solution 1 - Javascript

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>

Solution 2 - Javascript

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.

<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? Because you want css selectors that would target first option inside above select to target "real" option, not the one that's hidden. It gets useful when you're using protractor for e2e testing and (for whatever reason) you use by.css() to target select options.

Kevin - Sưu Tầm

Solution 3 - Javascript

You can forgo using the ng-options and use ng-repeat instead and make the first option blank. 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>

Solution 4 - Javascript

It appears that your best option would be to have the blank item included as the first item in itemlist.

Solution 5 - Javascript

The solution above will corrupt the model with junk data. Please use the directive to reset the model in the right way 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);

        }
    };
});

Solution 6 - Javascript

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.

also I set an initial value in my controller, like this :

$scope.item = 0;

Solution 7 - Javascript

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

Solution 8 - Javascript

Controller

$scope.item = '';

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

HTML

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionarnoldView Question on Stackoverflow
Solution 1 - JavascriptgeniuscarrierView Answer on Stackoverflow
Solution 2 - JavascriptKevin BlackView Answer on Stackoverflow
Solution 3 - Javascriptm.e.conroyView Answer on Stackoverflow
Solution 4 - JavascriptJuliane HolztView Answer on Stackoverflow
Solution 5 - Javascriptuser5971409View Answer on Stackoverflow
Solution 6 - JavascriptMehri RahnamaView Answer on Stackoverflow
Solution 7 - Javascriptimage72View Answer on Stackoverflow
Solution 8 - JavascriptKhaled EbdelliView Answer on Stackoverflow