javascript - 使用 Promises 时,Angular UI Typeahead 限制结果

标签 javascript angularjs angular-ui-bootstrap

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

当在标记中使用内联 promise 时,如下所示:

<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>

如果您在 Controller 中输入“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.

关于javascript - 使用 Promises 时,Angular UI Typeahead 限制结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39351528/

相关文章:

javascript - 如何将时区日期转换为正确的本地日期?

javascript - 如何将我的上下文变量传递给 Django 中的 javascript 文件?

css - Angular UI 选项卡,在移动设备中为假

javascript - 如何检查 Bootstrap UI 模式是否打开? - Angular JS

javascript - 专注于关闭模态框或弹出窗口

javascript - jQuery 按钮不执行?

javascript - 需要帮助将 jQuery 转换为 VanillaJS

angularjs - 处理关系属性时获取/设置实体

javascript - AngularJS:注入(inject) $timeout 在指令中不可用

angularjs - Angular-UI $dialog 和表单按 Enter 键提交