AngularJS - Set default value on select inside a ng-repeat [duplicate]

Multi tool use
AngularJS - Set default value on select inside a ng-repeat [duplicate]
This question already has an answer here:
I'm facing to an issue with AngularJS - I'm not able to display the selected value in a <select>
.
<select>
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
{{i.id}}
--- Select an option ---
{{value.value}}
And the objects:
$scope.vatRates = [
{ 'id': 1, 'value' : '20' },
{ 'id': 2, 'value' : '10' },
{ 'id': 3, 'value' : '7' }
];
$scope.items = [
{ 'id': 1, 'title' : 'Title1', 'vatRateId' : '1' },
{ 'id': 2, 'title' : 'Title2', 'vatRateId' : '2' },
{ 'id': 3, 'title' : 'Title3', 'vatRateId' : '3' }
];
Here is a live demo to explain my issue:
Demo on PLNKR
I'm not able to set to each item in the select the right value.
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
updateVatRate
settings
Thanks @NewDev, I thought I was clear enought ^^ nevermind here is a live demo to explain my issue. plnkr.co/edit/OROLI64Q13PlJhSE4GOl?p=preview I'm not able to set to each item in the select the right value
– Pat
May 7 '15 at 6:49
it's good that you have a demo, but the question should stand on its own - I updated it, fix the edit if you feel it doesn't reflect your actual issue.
– New Dev
May 7 '15 at 6:58
The accept answer has too many problems to be useful to other readers.
– georgeawg
Jul 2 at 23:29
4 Answers
4
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 == value.id}}" ng-repeat="value in vatRates">
<select>
<select>
<options ng-selected="{{defaultvalue == value.id}}" ng-repeat="value in vatRates">
For code and reference i just altered your plunker,
http://embed.plnkr.co/ZXo8n0jE8r3LsgdhR0QP/preview
Hope this helps.
Yes, this is exactly what I needed !!! Thanks a lot !! ;)
– Pat
May 7 '15 at 10:18
Happy that your issue got solved!! :)
– Alhuck A
May 7 '15 at 10:39
Just in case you are setting the default to a known string value, such as "Matt", you can't just use
ng-selected="'Matt'"
. Your known string needs to be equated to the value, i.e. ng-selected="{{ value == 'Matt' }}"
– Matthew
Jan 27 '16 at 20:13
ng-selected="'Matt'"
ng-selected="{{ value == 'Matt' }}"
If you are using
ngModel
on the select, you should not use ngSelected
on the options, as ngModel
will set the select value and selected options. See AngularJS ng-selected Directive API Reference.– georgeawg
Jul 2 at 23:14
ngModel
ngSelected
ngModel
The
ng-selected
attribute should not have interpolation with curly brackets {{ }}
. See AngularJS Developer Guide - Binding to boolean attributes. See also AngularJS Developer Guide - Why mixing interpolation and expressions is bad practice.– georgeawg
Jul 2 at 23:23
ng-selected
{{ }}
You should set the default value on the ng-model
via controller instead of ng-init. From the docs
ng-model
The only appropriate use of ngInit is for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.
— AngularJS ng-init Directive API Reference - Overview
angular.module('app', )
.controller('SelectController', function() {
var selectCtrl = this;
selectCtrl.values = ["Value 1", "Value 2", "Value 3"];
selectCtrl.selectedValue = "Value 1";
})
https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js
{{value}}
but then you can't switch option
– ParPar
Dec 7 '16 at 14:03
This is the only way i managed to do it, and its done when another select is changing. (maybe thats the cause other solutions don't work, please note the extra ""+ to force it to be a string. (otherwise not working)
In the controller:
$scope.formData.number_of_persons = "̲"̲+̲ $scope.numberOfPersons[0].val;
HTML:
<select name="number_of_persons"
id="number_of_persons"
ng-model="formData.number_of_persons"
required>
<option ng-repeat="option in numberOfPersons" value="{{option.val}}">
{{option.val}}
</option>
</select>
The other option is to use the
ng-value
directive which allows the value to be set to something other than a string.– georgeawg
Jul 3 at 0:17
ng-value
See AngularJS
select
Directive API Reference - Using ngValue
to bind the model to an array of objects.– georgeawg
Jul 3 at 0:21
select
ngValue
<select ng-model="val" ng-init="myval = vals[0]">
<option ng-repeat="myval in vals" value="{{myval.value}}">{{val.name}}
</option>
</select>
For more information, see AngularJS API Reference - Using ngRepeat to generate select options.
– georgeawg
Jul 3 at 0:22
Sure, we could try to help you. But can you help us first? Help us by removing the irrelevant parts of the markup and styling? Then, to avoid guesses and assumptions, give us an idea of the structure of some of the objects you use and some functions, like
updateVatRate
orsettings
? For extra bonus, maybe translate some variables/text into English (seeing how this is an English-language site). I fixed some things for you - please add more details about the missing code and next time, please invest time in your question.– New Dev
May 7 '15 at 6:18