gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:16:11 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com