Angularjs autocomplete from $http

前端 未结 5 2120
-上瘾入骨i
-上瘾入骨i 2020-11-29 16:52

I\'m trying to write an autocomplete directive that fetches data from the server using an $http request (without using any external plugins or scripts). Cur

5条回答
  •  情深已故
    2020-11-29 17:10

    I made an autocomplete directive and uploaded it to GitHub. It should also be able to handle data from an HTTP-Request.

    Here's the demo: http://justgoscha.github.io/allmighty-autocomplete/ And here the documentation and repository: https://github.com/JustGoscha/allmighty-autocomplete

    So basically you have to return a promise when you want to get data from an HTTP request, that gets resolved when the data is loaded. Therefore you have to inject the $qservice/directive/controller where you issue your HTTP Request.

    Example:

    function getMyHttpData(){
      var deferred = $q.defer();
      $http.jsonp(request).success(function(data){
        // the promise gets resolved with the data from HTTP
        deferred.resolve(data);
      });
      // return the promise
      return deferred.promise;
    }
    

    I hope this helps.

提交回复
热议问题