Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS : Remove empty select option in angular ng-repeat

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.

like image 682
Anshad Vattapoyil Avatar asked Jan 26 '14 08:01

Anshad Vattapoyil


1 Answers

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/

like image 91
Alex Choroshin Avatar answered Sep 27 '22 20:09

Alex Choroshin