问题描述
我对 UI-Bootstrap 的预输入有问题,主页上有一个标准示例,演示预输入的用法:
Hi, I have a problem with UI-Bootstrap's typeahead, there is standard example on the homepage that demonstrates typeahead usage:
http://plnkr.co/edit/LS5OMsnQtdsJ87eW4pjG?p=preview
这个例子工作得很好,直到我开始使用带有 typeahead
指令的 promise:http://plnkr.co/edit/ZuUBDOPcOJIW0Bkskrb5?p=preview
This example works pretty fine until I start using promises with typeahead
directive:http://plnkr.co/edit/ZuUBDOPcOJIW0Bkskrb5?p=preview
更改非常简单,我使用 $timeout 服务将直接变量初始化替换为延迟初始化,结果 typeahead 停止工作
The change is pretty simple, I've replaced direct variable initialisation with delayed initialisation using $timeout service, as a result typeahead stops working
我做错了什么?明确指出 UI-Bootstrap 的 typeahead: 与 promise 一起工作,这意味着您可以使用 $http 服务以最少的努力检索匹配
What am I doing wrong? It is clearly stated that UI-Bootstrap's typeahead: works with promises and it means that you can retrieve matches using the $http service with minimal effort
谢谢,
推荐答案
你应该返回一个解析为匹配结果的 promise:
You should be returning a promise that resolves to matched results:
$scope.getStates = function($viewValue) {
return $timeout(function () {
return filterFilter(['Alabama', 'Alaska', ...], $viewValue);
}, 1000);
};
然后在 HTML 中:
and then in HTML:
<input type="text" ng-model="selected" typeahead="state for state in getStates($viewValue)">
这是一个有效的 plunk:http://plnkr.co/edit/RAkzX0UoWHVLUOZ6jEyA?p=预览
Here is a working plunk: http://plnkr.co/edit/RAkzX0UoWHVLUOZ6jEyA?p=preview
您在过滤的 Promise 中编写表达式的方式.
The way you've written your expression in a promise being filtered.
这篇关于UI-bootstrap typeahead 不能解决承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!