Pat Pat - 1 year ago 109
AngularJS Question

AngularJS - Set default value on select inside a ng-repeat

I'm facing to an issue with AngularJS - I'm not able to display the selected value in a


Here is my use case:

I have a view which starts by an ng-repeat to display components. Each component contains a select to choose the vat rate. When creating new items, it looks fine. But when I'm editing existing items, the actual vatRate code is not displayed in the select, and instead I see the default option "-- Select VAT Rate --" instead of the selected VAT.

My model only contains the Id of the vat Rate.

With a single component I can use a variable in the $scope to set the current item value, but here I can have multiple components, each of them has its own vat rate, so I am not sure how do that here:

Here is my code

<div ng-repeat="c in components" ng-form="vatForm">
<select name="selectedVatRate"
ng-init="vatRate ="
ng-change="updateVatRate(c, selectedVatRate)"
ng-options=" for v in vatRates track by">
<option value="">-- Select VAT Rate --</option>

And the objects:

$scope.vatRates = [
{ 'id': 1, 'value' : '20' },
{ 'id': 2, 'value' : '10' },
{ 'id': 3, 'value' : '7' }

$scope.components = [
{ 'id': 1, 'title' : 'Title1', 'vatRateId' : '1' },
{ 'id': 2, 'title' : 'Title2', 'vatRateId' : '2' },
{ 'id': 3, 'title' : 'Title3', 'vatRateId' : '3' }


Answer Source

I am not very much clear about your requirement but if you want to display a default selected value to <select>, you can use ng-selected. In order to use that you need to have default selected value in your controller as a model to your <select> and add ng-selected to your <options ng-selected="{{defaultvalue ==}}" ng-repeat="value in vatRates">

For code and reference i just altered your plunker,

Hope this helps.