Typeahead:为什么在事件$ typeahead.select之后向服务器发送请求?

时间:2014-10-03 13:11:15

标签: javascript angularjs typeahead.js angular-strap

预先将请求发送到具有所选值的服务器。 我认为没有必要,它是如何阻止的?

Example Plnkr

<form class="form-inline" role="form">
  <div class="form-group">
    <label><i class="fa fa-home"></i> Address <small>(async via maps.googleapis.com)</small></label>
    <input type="text" class="form-control" ng-model="selectedAddress" data-animation="am-flip-x" ng-options="address as address for address in getAddress($viewValue)" template="a_template.html" placeholder="Enter address" bs-typeahead>
  </div>
  <div>
    selectedAddress: {{ selectedAddress }}
  </div>
</form>

- js的

$scope.getAddress = function(viewValue) {
  var params = {address: viewValue, sensor: false};
  return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {params: params})
  .then(function(res) {
    angular.forEach(res.data.results, function(o) {
      o.label = o.formatted_address;
    });
    console.log(res.data.results)
    return res.data.results;
  });
};

1 个答案:

答案 0 :(得分:0)

使用typeahead-min-length和typeahead-wait-ms。例如

      <input typeahead-min-length="3" typeahead-wait-ms="1000" ....

在输入至少3个字符且暂停时间为1000毫秒之前,不会对您的功能进行调用。 IE用户停止输入那么长时间。根据您的意愿玩这些值,以帮助减少所做的通话次数。