5

In my app I am using angular.js and jquery ui autocomplete. I had the same problem that is discussed HERE The accepted answer there works great for me and is exactly what I needed up until today when I need to replace the static array of values with an $http ajax call. I tried to pass $http as parameter to the parent function but I get "Unknown provider: autoCompleteProvider <- autoComplete"

My question is, how can I use $http without rewriting or changing too much the current solution?

2 Answers 2

12

You need to add a callback reference in your getSource() function of your service:

app.factory('autoCompleteDataService', ['$http', function($http) {
   return {
       getSource: function(callback) {
          var url = '...';
          $http.get(url).success(function(data) {
             callback(data);
          }
       }
   }
}]);

You could also use $http.jsonp, if your server returns json. Don't forget the JSON_CALLBACK parameter then.

In you directive you need to add the callback function itself:

...
autoCompleteDataService.getSource(function(data) {
   elem.autocomplete({
         source: data
         minLength: 2
   });    
});
Sign up to request clarification or add additional context in comments.

5 Comments

did it and now i get "TypeError: this.source is not a function"
Can you setup an example then, since I don't know how you are using the $hhtp service
i'm sorry i had a typo, i fixed it and now there is no errors, but also no data returns. i use the $http service exactly like in your answer above.
yes of course i have replaced the url to my web service url and i can see in firebug that the service is returning data successfully, but the autocomplete is not working and there is no js error
asgoth - your answer was correct. the problem was that my data was array of objects: [{city_id:x, city_name:y}], after changing it to: [{key:x, value:y}] the problem solved, how bizzare!
0

This is how you can do it:

app.factory('autoCompleteDataService', ['$http', function($http) {
    return {
        getSource: function() {
            return function(request, response) {
                $http.get(url).success(function(data) {
                    response(data);
                });
            }
        }
    }

}]);

However, if you want to download the entire data first and allow the autocomplete widget to search the data on the client side, this is an example how you can do it:

app.directive('autoComplete', function(autoCompleteDataService, $http) {
return {
    restrict : 'A',
    link : function(scope, elem, attr, ctrl) {
        autoCompleteDataService.getData(function(err, data) {
            if (err) {
                console.log("Could not retrieve data.");
                return;
            }

            elem.autocomplete({
                minLength: 0,
                source: data,
                focus: function( event, ui ) {
                    elem.val( ui.item.label );
                    return false;
                },
                select: function( event, ui ) {
                    elem.val( ui.item.label );
                    return false;
                }
            })
            .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
                return $( "<li>" )
                .append( "<a>" + item.label + "</a>" )
                .appendTo( ul );
            };
        });
    }
};

});

app.factory('autoCompleteDataService', ['$http', '$rootScope', function($http, $scope) {
return {
    getData: function(callback) {
        if ($scope.data) {
            return callback(null, $scope.data);
        }

        $http.get('URL')
        .success(function(data) {
            $scope.data = data;
            return callback(null, data);
        })
        .error(function(data) {
            return callback(true, null);
        });
    }
}

}]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.