Javascript 如何为angular ui-select设置默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27623490/
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
how to set a default value for angular ui-select
提问by praveenpds
Question :
题 :
how to set default value to the angular ui-select
如何将默认值设置为角度 ui-select
drop down values are fetched from object and object wont be having default value.
下拉值是从对象中获取的,对象不会有默认值。
and ui-select should set the default value in Frontend.
和 ui-select 应该在 Frontend 中设置默认值。
eg:
例如:
drop down values are as follows
下拉值如下
1 -all fruits2 -apple 3 -banana 4 -water melon
1 - 所有水果2 - 苹果 3 - 香蕉 4 - 西瓜
value from 2 to 4 are derived from object sent from server.but Frontend need to set default value ie 1 - all fruits
从 2 到 4 的值来自从服务器发送的对象。但前端需要设置默认值,即 1 - 所有水果
Referring to the below example set via ui-select2 how to migrate this in ui-select?
参考下面通过 ui-select2 设置的示例如何在 ui-select 中迁移它?
<select ng-model="fID" class="select" ui-select2 data-placeholder="select fruit">
<option value="">All fruits</option>
<option ng-repeat="f in frits value="{{f.fID}}">{{f.name}}</option>
</select>
<ui-select theme="select2" ng-model="fruits.selected">
<ui-select-match placeholder="select please" allow-clear="false"> {{$select.selected.name}}
</ui-select-match>
<ui-select-choices repeat="f in fruits | filter:$select.search">
<div ng-bind-html="f.name | highlight: $select.search:'underline'"></div>
</ui-select-choices>
</ui-select>
http://plnkr.co/edit/a3KlK8dKH3wwiiksDSn2?p=previewhttps://github.com/angular-ui/ui-selectLink :angular-ui/ui-select
http://plnkr.co/edit/a3KlK8dKH3wwiiksDSn2?p=preview https://github.com/angular-ui/ui-select链接:angular-ui/ui-select
回答by bahadir
you didnt use an id or something like that for option value so ui-select compares object address to understand if it is selected.
你没有使用 id 或类似的东西作为选项值,所以 ui-select 比较对象地址以了解它是否被选中。
var p1 = { name: 'Adam', email: '[email protected]', age: 10 };
$scope.person = {selected: p1};
$scope.people = [
p1,
{ name: 'Amalie', email: '[email protected]', age: 12 },
{ name: 'Wladimir', email: '[email protected]', age: 30 },
{ name: 'Samantha', email: '[email protected]', age: 31 },
{ name: 'Estefanía', email: 'estefaní[email protected]', age: 16 },
{ name: 'Natasha', email: '[email protected]', age: 54 },
{ name: 'Nicole', email: '[email protected]', age: 43 },
{ name: 'Adrian', email: '[email protected]', age: 21 }
];
change plunker like this and you will have a default selected value.
像这样更改plunker,您将拥有一个默认的选定值。
to set the default value on view you can use ng-init and set first object as selected
要在视图中设置默认值,您可以使用 ng-init 并将第一个对象设置为选中状态
回答by Aqib Mumtaz
I found this working example: http://jsfiddle.net/NfPcH/28/
我找到了这个工作示例:http: //jsfiddle.net/NfPcH/28/
Angular code:
角度代码:
<a href="#" editable-select="user.status" e-ng-options="s.value as s.text for s in statuses">
{{ showStatus() }}
</a>
Controller:
控制器:
app.controller('Ctrl', function($scope, $filter) {
$scope.user = {
status: 2
};
$scope.statuses = [
{value: 1, text: 'status1'},
{value: 2, text: 'status2'},
{value: 3, text: 'status3'},
{value: 4, text: 'status4'}
];
$scope.showStatus = function() {
var selected = $filter('filter')($scope.statuses, {value: $scope.user.status});
return ($scope.user.status && selected.length) ? selected[0].text : 'Not set';
};
});
回答by Naren
In your controller add the following code:
在您的控制器中添加以下代码:
$scope.fruits.selected = {name: 'All fruits'};
The above will set the default selected value to the drop down.
以上将默认选择的值设置为下拉菜单。

