我正在使用角度ui-select进行多重选择。
<label>All Users</label>
<ui-select multiple ng-model="a.users" close-on-select="false">
<ui-select-match placeholder="Select users">{{$item}}</ui-select-match>
<ui-select-choices typeahead="val for val in getAllUsers($viewValue)" typeahead-loading="loadingCodes" typeahead-no-results="noResults"></ui-select-choices>
</ui-select>
下拉列表中的数据来自API。
我的指令代码:
scope.getAllUsers = function(key) {
var obj = {
"key": key
}
function extract(resp) {
return resp.data.slice(0)
}
if (key.length >= 2) {
return Promise.all([
ApiServices.getPrimaryUsers(obj).then(extract),
ApiServices.getSecondaryUsers(obj).then(extract)
])
.then(function(results) {
return [].concat.apply([], results)
});
}
else {
return false;
}
}
但我不适合我。我没有在下拉列表中获得任何数据。无法多次选择。任何人都可以帮我这个吗?
答案 0 :(得分:2)
试试这个,它应该按照你的预期工作。在您的视图中,添加以下代码。
<label>All Users</label>
<ui-select multiple ng-model="a.users" close-on-select="false">
<ui-select-match placeholder="Select users">{{$item.name}}</ui-select-match>
<ui-select-choices minimum-input-length="1" repeat="user in filteredUsers track by $index" refresh="refreshUsers($select.search)" refresh-delay="0">
<div ng-bind-html="user.name | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
<pre>{{a.users}}</pre>
在控制器中,添加以下代码。您可以返回始终充当Promise
的{{1}}响应,而不是使用getUsers()
中的$http
返回静态数组对象。
Promise