Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  42] [ 7]  / answers: 1 / hits: 47154  / 9 Years ago, wed, october 7, 2015, 12:00:00

I am using angular ui-select for drop down. How can I set a default value for the drop down value?



<h3>Selectize theme</h3>
<p>Selected: {{country.selected}}</p>
<ui-select ng-model=country.selected theme=selectize ng-disabled=disabled style=width: 300px;>
<ui-select-match placeholder=Select or search a country in the list...>{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat=country in countries | filter: $select.search>
<span ng-bind-html=country.name | highlight: $select.search></span>
<small ng-bind-html=country.code | highlight: $select.search></small>
</ui-select-choices>
</ui-select>


The snippet is from plnkr.co. Currently the dropdown just sowing default Select or search a country in the list... But i need to pass a value from controller. Lets say $scope.default = {name:Decard}



Thanks!!



EDIT



This question is similar to This one but involving json return format of data.


More From » angularjs

 Answers
36

You can initial your ng-model to the default country object in your controller as following:



 $scope.country = {};
$scope.country.selected = {name: 'Albania', code: 'AL'};


Then use ui-select-match to set the default value like this:



<ui-select ng-model=country.selected theme=selectize ng-disabled=disabled style=width: 300px;>
<ui-select-match placeholder=Select or search a country in the list...>{{ $select.selected.name }}</ui-select-match>
<ui-select-choices repeat=country in countries | filter: $select.search>
<span ng-bind-html=country.name | highlight: $select.search></span>
<small ng-bind-html=country.code | highlight: $select.search></small>
</ui-select-choices>
</ui-select>



[#64814] Monday, October 5, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ignacio

Total Points: 467
Total Questions: 128
Total Answers: 79

Location: Luxembourg
Member since Tue, Mar 14, 2023
1 Year ago
ignacio questions
;