0

It is a frequent question but I don't know the answer after checking lots of blogs and stackoverflow questions.
I want to select a default value in a select box and the values of the select comes from the server ($resource). I cant set the ngModel of the select in the controller as I don't have the list yet (I only have a promise). Also I tried to configure the ngInit but no success.

Could anybody help how to set the default value of a select to first element of a list coming from server?

2 Answers 2

1

You can maybe do something like that :

<select ng-model="selectedValue" ng-options="o.value for o in values">

And in the return of the promise :

scope.values = response.data;
scope.selectedValue = scope.values[0];
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Digix, thanks for the response! I am using a service for the communication and I already handle the promise in that service. It is a general service for all the communication so I cant 'overwrite' the promise when feeding the combo box.
So, maybe you can expose a "model" in the service like : myService = { model : {data:null,selectedValue:null} }, and then put that model in the controller scope like : scope.model = myService.model
0

You can hide the select input until the promise is finished and show it only after. I usually show a loader gif until all my promises are finished and only then display all the content so there are no empty zones in the page. Also, be careful to set the value of the ng-model to be identical to one of the options value.

2 Comments

Hi Cristi, thanks for the response! I am using a service for the communication and I already handle the promise in that service. It is a general service for all the communication so I cant 'overwrite' the promise when feeding the combo box. Is there any other solution for that? I thought there is a 'built-in' solution for handling problems like that.
You can just set the value for the ngModel when resolving the promise. Can you post a code snippet? Btw, someone asked the same thing here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.