初始化ng-model时,角度ui-select占位符不起作用 [英] Angular ui-select placeholder not working when ng-model is initialized
问题描述
我试图每次单击按钮时添加一个新选择,
I am trying to add a new select every time a button is clicked,
html:
<div ng-repeat = "select in selects track by $index">
<ui-select ng-model="selects[$index]" theme="select2" ng-disabled="disabled" style="min-width: 300px;">
<ui-select-match placeholder="Select a person in the list or search his name/age...">{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="person in people">
<div ng-bind-html="person.name | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
</div>
<button ng-click="addNewSelect()"> Add new select</button>
控制器:
$scope.selects = [{}];
$scope.addNewSelect = function() {
$scope.selects.push({});
}
对象数组已保存在数组选择"中,但是在我最初使用空对象初始化ng-model时,占位符未出现在选择中.在这种情况下如何让他的占位符起作用?
The array of objects gets saved in the array 'selects', but the placeholder is not coming in the selects as I am initializing the ng-model with an empty object initially. How to get he placeholder working in this case?
这是柱塞.
推荐答案
在selects[$index]
之后缺少.selected
.
否则,用户界面选择会认为您选择了一个空对象(selects[$index]
),并且不会显示占位符.
Without this, the ui-select believes that you have selected an empty object (the selects[$index]
) and won't show the placeholder.
<ui-select ng-model="selects[$index].selected" theme="select2" ng-disabled="disabled" style="min-width: 300px;">
http://plnkr.co/edit/56SHyE01BJ4EXglLlIcb?p=preview
您也不需要使用索引进行查找,只需使用select.selected
also you dont need to look up using the index, you can just use select.selected
<ui-select ng-model="select.selected" theme="select2" ng-disabled="disabled" style="min-width: 300px;">
http://plnkr.co/edit/3Uq8lDtDOaj5mIZVrCfv?p=preview
这篇关于初始化ng-model时,角度ui-select占位符不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!