ui-select 有多个和异步选项的问题
Problem with ui-select with multiple and async options
我对 ui-select 指令有疑问(AngularJS 1.6.4,Ui-select 0.19.8)。
我创建了 fiddle here.
如果输入超过 3 个字符,它应该会在下拉列表中显示联系人。 (目前我什至没有尝试过滤任何内容,返回的是相同的联系人列表)。第一次效果很好,然后其他时候没有显示下拉菜单。
我使用异步方式返回结果,因此它会在 1 秒后调用我的 "refresh" 函数。
谁能帮我理解为什么第一次后没有显示下拉菜单?
(另外,如果有人知道为什么我需要为 <ul>
标签强制使用 display: block
- 参见 CSS )
谢谢
HTML
<body ng-app="myApp">
<div body ng-controller="ctrl as ctrl">
<div class="element">
Selected Contacts:<br>
<div ng-repeat="contact in ctrl.contacts" class="contact">
{{ contact }}
</div>
</div>
<ui-select multiple ng-model="ctrl.contacts" class="element">
<ui-select-match placeholder="Pick one...">{{$item}}</ui-select-match>
<ui-select-choices
position="down"
refresh="ctrl.refreshContacts($select.search)"
refresh-delay="1000"
minimum-input-length="3"
repeat="person in ctrl.people">
<div ng-bind-html="person | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
<div class="element">
<div ng-repeat="log in ctrl.logs track by $index" >
<div>
{{log}}
</div>
</div>
</div>
</div>
</body>
JS
var myApp = angular.module('myApp', ['ngSanitize','ui.select']);
myApp.controller("ctrl", [function () {
var ctrl = this;
ctrl.logs=[];
ctrl.refreshContacts = function(search) {
var people = [
"mickael",
"pierre",
"anna",
"alice",
"bob"
];
ctrl.people = people;
ctrl.logs.push("refreshContacts called")
}
ctrl.people = [];
}]);
CSS
.element {
margin-bottom: 20px;
}
.contact {
display: inline-block;
margin: 5px;
padding: 5px 8px;
background: grey;
}
/* why do I need this ?! */
.ui-select-choices {
display: block;
}
如果我没记错的话,在同时使用 minimum-input-length 和刷新时会出现错误。我通过删除 minimum-input-length 并在刷新函数中添加一个 if 语句解决了这个问题。
ctrl.refreshContacts = function(search) {
if(search == undefined || search.length < 3){
return;
}
else{
people = [
"mickael",
"pierre",
"anna",
"alice",
"bob"
];
ctrl.people = people;
}
}
我对 ui-select 指令有疑问(AngularJS 1.6.4,Ui-select 0.19.8)。
我创建了 fiddle here.
如果输入超过 3 个字符,它应该会在下拉列表中显示联系人。 (目前我什至没有尝试过滤任何内容,返回的是相同的联系人列表)。第一次效果很好,然后其他时候没有显示下拉菜单。
我使用异步方式返回结果,因此它会在 1 秒后调用我的 "refresh" 函数。
谁能帮我理解为什么第一次后没有显示下拉菜单?
(另外,如果有人知道为什么我需要为 <ul>
标签强制使用 display: block
- 参见 CSS )
谢谢
HTML
<body ng-app="myApp">
<div body ng-controller="ctrl as ctrl">
<div class="element">
Selected Contacts:<br>
<div ng-repeat="contact in ctrl.contacts" class="contact">
{{ contact }}
</div>
</div>
<ui-select multiple ng-model="ctrl.contacts" class="element">
<ui-select-match placeholder="Pick one...">{{$item}}</ui-select-match>
<ui-select-choices
position="down"
refresh="ctrl.refreshContacts($select.search)"
refresh-delay="1000"
minimum-input-length="3"
repeat="person in ctrl.people">
<div ng-bind-html="person | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
<div class="element">
<div ng-repeat="log in ctrl.logs track by $index" >
<div>
{{log}}
</div>
</div>
</div>
</div>
</body>
JS
var myApp = angular.module('myApp', ['ngSanitize','ui.select']);
myApp.controller("ctrl", [function () {
var ctrl = this;
ctrl.logs=[];
ctrl.refreshContacts = function(search) {
var people = [
"mickael",
"pierre",
"anna",
"alice",
"bob"
];
ctrl.people = people;
ctrl.logs.push("refreshContacts called")
}
ctrl.people = [];
}]);
CSS
.element {
margin-bottom: 20px;
}
.contact {
display: inline-block;
margin: 5px;
padding: 5px 8px;
background: grey;
}
/* why do I need this ?! */
.ui-select-choices {
display: block;
}
如果我没记错的话,在同时使用 minimum-input-length 和刷新时会出现错误。我通过删除 minimum-input-length 并在刷新函数中添加一个 if 语句解决了这个问题。
ctrl.refreshContacts = function(search) {
if(search == undefined || search.length < 3){
return;
}
else{
people = [
"mickael",
"pierre",
"anna",
"alice",
"bob"
];
ctrl.people = people;
}
}