Angular UI 使用 Promises 时预先输入限制结果

Angular UI Typeahead Limit Results when using Promises

我fork了一个Plunker作为例子http://plnkr.co/edit/ylR1mel46FXTXzdvfHpa?p=preview

像这样在标记中使用内联承诺时:

<div class='container-fluid' ng-controller="TypeaheadCtrl">
    <pre>Model: {{selected| json}}</pre>
    <input type="text" ng-model="selected" typeahead="state for state in getStates($viewValue) | limitTo: 5">
</div>

如果您在控制器中输入 "A",您会看到 limitTo 选项被忽略了。可以这么用吗?

您需要在 promise 的回调中限制结果。 https://github.com/angular-ui/bootstrap/issues/1740

AngularJS filters are executing synchronously and if you write your expression like address for address in getLocation($viewValue) | filter:$viewValue this part getLocation($viewValue) means that a filter would filter a promise object (which is noop as filter filter can only operate on arrays). The correct approach here is to do filtering on the server-side (this is the whole point of having async results from the server). If you still want to do further filtering on the client side you can do so in the promise callback function, in JavaScript.