简体   繁体   English

使用promise和ordera预先输入angular-ui引导程序

[英]angular-ui bootstrap typeahead with promise and orderBy

I am trying to use the typeahead directive with Angular-ui bootstrap as described here: http://angular-ui.github.io/bootstrap/#/typeahead 我正在尝试将typeahead指令与Angular-ui引导程序一起使用,如下所述: http : //angular-ui.github.io/bootstrap/#/typeahead

My end goal is to make an $http call to return an array of objects. 我的最终目标是进行$ http调用以返回对象数组。 I have this working well, however I am not able to get the orderBy or limitTo filters to work. 我的工作正常,但是我无法使orderBy或limitTo过滤器正常工作。

I have reproduced a similar result using promises in the following: http://plnkr.co/edit/AFPjWArALkZU5ImzgobG?p=preview 我在以下使用Promise复制了类似的结果: http : //plnkr.co/edit/AFPjWArALkZU5ImzgobG?p=preview

Both results should be sorted by state name descending, but only the one populated by an object is actually doing so. 两种结果都应按状态名称降序排序,但实际上只有对象填充的结果才这样做。

My question is whether I am missing something or if this is a known limitation? 我的问题是我是否缺少某些东西,或者这是否是已知限制? Are there any workarounds? 有什么解决方法吗? I know I could do the sorting in the controller using "then" after the promise, but that seems a bit clunky. 我知道我可以在诺言之后使用“ then”在控制器中进行排序,但这似乎有些笨拙。

The way you've written it was making a filter acting on the promise itself, rather on the result of the promise. 您编写它的方式是使过滤器作用在promise本身上,而不是作用在promise的结果上。 The proper approach in this case is to do the actual filtering in JavaScript, as shown below: 在这种情况下,正确的方法是使用JavaScript进行实际过滤,如下所示:

$scope.getStates = function(val) {
    return $timeout(function(){
      return orderByFilter(filterFilter($scope.statesWithFlags, val), 'name', true);
    }, 500)
  };

As a side note: the $timeout service API is already promise-based so no need to use $q explicitly. 附带说明: $timeout服务API已经基于承诺,因此无需显式使用$q

Here is a plunk: http://plnkr.co/edit/w5rhFKgYKNOQ6FFVNoUY?p=preview 这是一个小问题: http ://plnkr.co/edit/w5rhFKgYKNOQ6FFVNoUY? p= preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM