AngularJS : Remove empty select option in angular ng-repeat

All we need is an easy explanation of the problem, so here it is.

I am using ng-repeat to render options with different value and text here, and also setting the default one.But again angular adds an empty undefined option.

<select ng-model="newItem.financial_year_id" required style="min-width:180px;">
   <option ng-repeat="financial in account_year" value="{{financial.id}}" ng-selected="financial.status=='Active'">{{financial.financial_year}}</option>
</select>

Now the active one is selected but the empty option still rendering.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

The empty option is generated when a value referenced by ng-model doesn’t exist in a set of options passed to ng-options.

You can find the full answer+example on stackoverflow. here’s the link

UPDATE:

here’s an example:

 <select ng-model="newItem.financial_year_id" required style="min-width:180px;">
   <option ng-repeat="financial in account_year" value="{{financial.id}}" ng-selected="financial.status=='Active'">{{financial.financial_year}}</option>
</select>

in controller:

$scope.newItem={};
$scope.account_year=[{id:1,financial_year:"2013-2014",status:"Active"},
                     {id:2,financial_year:"2014-2015",status:"Inactive"}] 
//remove empty option
$scope.newItem.financial_year_id=$scope.account_year[0].id;

live example:
http://jsfiddle.net/choroshin/5YDJW/7/

Method 2

Try ng-options instead:

<select ng-model="selectedFinancial" ng-options="c.financial_year for c in account_year"
        required style="min-width:180px;">
</select>

DEMO

You should think about redesigning your model like that to use ng-options. When we use <select>, there is only 1 selected item and this item should be a property of $scope referencing an item in the list => we don’t need to duplicate 1 more property with "active" and "inactive" for all objects.

Your current model design makes sense when there are more than 1 selected item, but if there are more than 1, we should use a list of checkboxes instead of <select>

If you want to bind with Id, try this:

<select ng-model="selectedFinancialId" ng-options="c.id as c.financial_year for c in account_year"
        required style="min-width:180px;">
</select>

DEMO

Method 3

Ng-option is also a good way. But if you really want ng-repeat then use like this.

  $scope.newItem.financial_year_id=" ";

Initialize the variable when you start the controller. If you not initialize it then it takes as undefined. So the first empty value is present.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply