简体   繁体   English

angular.js ui + bootstrap typeahead +异步调用

[英]angular.js ui + bootstrap typeahead + asynchronous call

I'm using typeahead with the angular.js directive but my function to populate the autocomplete makes an asynchronous call and I can't return it to populate the autocomplete. 我正在使用带有angular.js指令的typeahead,但我填充自动完成的函数会进行异步调用,我无法返回它来填充自动完成。 Is there anyway to make it work with this asynchronous call? 反正有没有让它与这个异步调用一起工作?

Can I assume that you are using the typeahead of Bootstrap 2.x ? 我可以假设您正在使用Bootstrap 2.x的类型?

If so, in the documentation, the description of the source field of typeahead() 's options is this: 如果是这样,在文档中, typeahead()的选项的source字段的描述是这样的:

The data source to query against. 要查询的数据源。 May be an array of strings or a function. 可以是字符串数组或函数。 The function is passed two arguments, the query value in the input field and the process callback. 该函数传递两个参数,即输入字段中的查询值和进程回调。 The function may be used synchronously by returning the data source directly or asynchronously via the process callback's single argument. 通过进程回调的单个参数直接或异步返回数据源,可以同步使用该函数。

You can definitely pass in an async function as the source attr. 你绝对可以传入一个异步函数作为source attr。 The source function could be something like: source函数可能是这样的:

function someFunction(query, process) {
  someAsyncCall(...query or so... , function(res) { // success callback
    process(res);
  }, function(err) {                                // error callback
    process(null);
  });
}

Update: 更新:

If you are using Angular Bootstrap's typeahead, it should be even easier. 如果您使用的是Angular Bootstrap的typeahead,它应该更容易。 According to Angular Bootstrap's docs( http://angular-ui.github.io/bootstrap/ ), you can just return a promise for the typeahead function. 根据Angular Bootstrap的文档( http://angular-ui.github.io/bootstrap/ ),您可以返回对typeahead函数的承诺。 Some example from the docs: 文档中的一些示例:

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

A simpler one could be: 一个更简单的可能是:

$scope.getSomething= function(query) {
  var promise = $http.get('...some url...', {
    params: {
      queryName: query
    }
  });
  return promise;
};

Or you can build your own promise: 或者你可以建立自己的承诺:

$scope.getSomething= function(query) {
  var deferred = $q.defer();
  someAsyncCall(...query or so... , function(res) { // success callback
    deferred.resolve(res);
  }, function(err) {                                // error callback
    deferred.reject(err);
  });
  return deferred.promise;
};

Actually, many services like $http are just returning promises when you call them. 实际上,许多服务,如$http ,只是在你打电话时返回承诺。 More about promise in AngularJS: https://docs.angularjs.org/api/ng/service/ $q 更多关于AngularJS中的承诺: https ://docs.angularjs.org/api/ng/service/ $ q

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

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