Hi,
What is the best approach for autocomplete in AngularJS? I have a web application built with AngularJS, and this application has an input field for searching. For this field I need some sort of autocomplete functionality, the data source for the autocomplete is a MVC4 Web API (remote datasource). Something that looks and behaves like the autocomplete on
www.google.com would have been nice.
I have researched this a bit, and have tried the following approaches:
1. jQuery UI Autocomplete. But this one does not play nicely together with AngularJS, I get an error message saying that .autocomplete does not exist for the element I'm trying to attach it to. But the look and feel of the jQuery UI autocomplete is very nice and clean, and just what I want.
2. AngularUI Select2 directive. But this looks and feels like a combobox, not a search autocomplete feature
4. AngularJS custom directive based on samples found in this group and elseewhere:
directive('cxAutocomplete', function () {
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<input id="searchbox" type="search" autofocus="true" size="50" spellcheck="true" ng-model="coxitoSearchParameter" />',
link: function (scope, element, attrs) {
scope.$watch(attrs.list, function (value) {
element.autocomplete({
source: value,
select: function (event, ui) {
scope[attrs.selection] = ui.item.value;
scope.$apply();
}
});
});
}
};
})
Calling this directive in HTML:
<cx-autocomplete selection="name" list="doGetAutocomplete" />
The doGetAutocomplete() method:
$scope.doGetAutocomplete = function (request, response) {
$scope.autocompleteResult = $scope.coxitoautocomplete.get({ MaxSuggestions: '5', UserQuery: $scope.coxitoSearchParameter }, $scope.doneAutocomplete(response));
};
I added a callback method ($scope.doneAutocomplete(response)) in the above function, with the intention of it being called when the data arrived from the remote datasource, but this callback function seems to be called immediately instead of waiting until data is returned. If this callback function had been called when I want it to be called then this solution would have worked just fine.
But the result of this directive and these functions is nothing, since it does not wait until data arrives from the remote datasource.
The $scope.doneAutocomplete(response) callback function looks like this, this is where I assign data with the response callback function:
$scope.doneAutocomplete = function (response) {
response($scope.autocompleteResult.Suggestions);
$scope.$apply();
};
Does anyone see what is wrong with the callback method here, why is it not called when the data is returned? Sorry that I cannot create a JSFiddle for this, since my datasource is not externally accessible.
So that's the four approaches I have tried. Note that I am not a Javascript expert, so I may have missed something obvious somewhere.
What is the best practice for implementing an autocomplete with a remote datasource in AngularJS?
Thanks for your help.
Best,
Stein J. Gran
PS: I sent this message to this Google group yesterday as well, but since I cannot see it in the group I post it again. Sorry for any double-posting.