限制预输入结果 [英] Limit typeahead results

查看:19
本文介绍了限制预输入结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 angular-ui typeahead 指令连接到 Google Maps API 并检索地址数组.通常,当我需要限制可见结果的数量时,我会执行以下操作:

I'm using the angular-ui typeahead directive to connect to the Google Maps API and retrieve an array of addresses. Normally when I need to limit the amount of results visible I do something like:

<input typeahead="eye for eye in array | filter:$viewValue | limitTo:10">

这很完美,结果被限制为 10.但是,当我尝试对异步结果做同样的事情时,它不起作用.它会给出比我在 limitTo 中指定的更多的结果.

That works perfectly and the results are limited to 10. However, when I try to do the same thing with asynchronous results, it doesn't work. It will give more results than I specified in the limitTo.

我在下面做错了什么吗?

Am I doing something incorrectly below?

这是一个笨蛋:

HTML:

  <input ng-model="asyncSelected" typeahead="address for address in getLocation($viewValue) | limitTo:1" typeahead-loading="loadingLocations">

JavaScript:

JavaScript:

  $scope.getLocation = function(val) {
    return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {
      params: {
        address: val
      }
    }).then(function(res){
      var addresses = [];
      angular.forEach(res.data.results, function(item){
        addresses.push(item.formatted_address);
      });
      return addresses;
    });
  };

目前我正在做以下解决方法,我只是好奇为什么简单的 limitTo 不起作用.

Currently i'm doing the following to workaround, i'm just curious why a simple limitTo doesn't work.

$scope.getLocation = function(val) {
    return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {
      params: {
        address: val
      }
    }).then(function(res){
      var addresses = [];
      var resultNumber = res.data.results.length > 5 ? 5 : res.data.results.length;
      for(var i = 0; i < resultNumber; i++){
        var obj = res.data.results[i];
        var addr = obj.formatted_address;
        addresses.push(addr);
      }
            return addresses;
    });
  };

推荐答案

typeahead 似乎不支持 promise.因此,最好将其绑定到一个集合,然后在收到来自 google 的响应时更新该集合.您可能想要考虑去抖动强硬,现在对输入的每个字母都进行请求.

typeahead doesn't seem to support promises. So it's better to just bind it to a collection, and then update that collection when you get a response from google. You might want to think about debouncing tough, now a request is done for every letter typed.

请注意,您也不再需要过滤器,因为过滤器已经由谷歌服务器端完成.

Note that you also don't need the filter anymore, because the filter is already being done by google sever side.

http://plnkr.co/edit/agwEDjZvbq7ixS8El3mu?p=preview

var app = angular.module('app',['ui.bootstrap']);

app.controller('Ctrl', ['$scope','$http', function($scope,$http){
  $scope.locations = [];
  $scope.$watch('asyncSelected', function(val) {
    $http.get('http://maps.googleapis.com/maps/api/geocode/json', {
      params: {
        address: val
      }
    }).then(function(res){
      $scope.locations.length = 0;
      angular.forEach(res.data.results, function(item){
        $scope.locations.push(item.formatted_address);
      });
    });
  });

}]);

  <head>
    <link data-require="bootstrap-css@~3.1.1" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
    <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
    <script data-require="ui-bootstrap@*" data-semver="0.11.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.min.js"></script>
    <script data-require="jquery@*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script data-require="bootstrap@*" data-semver="3.1.1" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="app" ng-controller="Ctrl">
    <input type="text" ng-model="asyncSelected" placeholder="Address" typeahead="address for address in locations | limitTo:1" typeahead-loading="loadingLocations" class="form-control">
  </body>

</html>

这篇关于限制预输入结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆