angular-ui bootstrap 提前输入限制不起作用

angular-ui bootstrap typeahead limit is not working

我是 angularjs 的新手。从这个网站 https://angular-ui.github.io/bootstrap/ 我得到了我的 angularjs 项目的 typeahead 指令,但是我的限制和过滤器在这里不起作用是我的代码。

当我在文本字段中键入 "c" 时,我只想显示 8 条记录,但显示了所有记录。

笨蛋 http://plnkr.co/edit/RhwHmKwSxWBh3rp0u85O?p=preview

angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('TypeaheadCtrl', function($scope, $http) {

  $scope.getLocation = function(val) {
    return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {
      params: {
        address: val,
        sensor: false
      }
    }).then(function(response){
      return response.data.results.map(function(item){
        return item.formatted_address;
      });
    });
  };
});
<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<script type="text/ng-template" id="customTemplate.html">
  <a>
      <img ng-src="http://upload.wikimedia.org/wikipedia/commons/thumb/{{match.model.flag}}" width="16">
      <span bind-html-unsafe="match.label | typeaheadHighlight:query"></span>
  </a>
</script>
<div class='container-fluid' ng-controller="TypeaheadCtrl">


    <h4>Asynchronous results</h4>
    <pre>Model: {{asyncSelected | json}}</pre>
    <input type="text" ng-model="asyncSelected" placeholder="Locations loaded via $http" typeahead="address for address in getLocation($viewValue) | filter:$viewValue  |limitTo:8" typeahead-loading="loadingLocations" class="form-control">
    <i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>

</div>
  </body>
</html>

我知道这是一个老问题,但我还是会继续回答以供将来参考:

这是通过 $http 加载时的已知行为,因为在调用您的 get 方法时限制将应用于承诺,这​​将导致它在您的承诺解决时不适用于实际结果集。

请参阅 ui.bootstrap 存储库中的问题 #1740 了解更多信息。

解决此问题的方法是滚动您自己的过滤器函数和 return 来自该过滤器的承诺而不是直接获取记录,或者在 API 中实施限制参数并限制它的服务器边.