Javascript AngularJS ui-select 下拉默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32983105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS ui-select dropdown default value
提问by d3bug3r
I am using angular ui-selectfor drop down. How can I set a default value for the drop down value?
我正在使用angular ui-select进行下拉。如何为下拉值设置默认值?
<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"}
该片段来自plnkr.co。目前,下拉列表只是播种默认值Select or search a country in the list...
但我需要从控制器传递一个值。可以说$scope.default = {"name":"Decard"}
Thanks!!
谢谢!!
EDIT
编辑
This question is similar to This onebut involving json return format of data.
这个问题类似于This one,但涉及数据的json返回格式。
回答by UserNeD
You can initial your ng-model to the default country object in your controller as following:
您可以将 ng-model 初始化为控制器中的默认国家/地区对象,如下所示:
$scope.country = {};
$scope.country.selected = {name: 'Albania', code: 'AL'};
Then use "ui-select-match" to set the default value like this:
然后使用“ui-select-match”设置默认值,如下所示:
<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>