What i'm trying to do is an Angular Material autocomplete (md-autocomplete) with data dynamically retrieved from an AJAX call to my REST API. Unfortunately I get only indeterminate progress bar instead of autocomplete items as you can see below.
Result
Controller
$scope.customersSelect = {};
$scope.selectedItem = null;
$scope.searchText = null;
$scope.getCustomers = function (query) {
selectsService.getCustomers(query).then(function (results) {
$scope.customersSelect = results.data;
console.log($scope.customersSelect);
}, function(error) {
alert(error.data.message);
});
}
Service
var selectsServiceFactory = {};
_getCustomers = function (query) {
return $http.get(serviceBase + 'api/selects/customers/' + query)
.then(function(results) {
return results;
});
}
selectsServiceFactory.getCustomers = _getCustomers;
return selectsServiceFactory;
View
<md-autocomplete md-floating-label="Klient"
autocomplete="off"
flex=""
md-search-text-change="getCustomers(searchText)"
md-item-text="item"
md-items="item in customersSelect"
md-search-text="searchText"
md-selected-item="machine.customerId"
md-input-maxlength="100"
md-input-minlength="2"
md-input-name="machineOwner">
<md-item-template>
<span md-highlight-text="searchText">{{item}}</span>
</md-item-template>
I'm getting the data successfully from the API, because I can see it printed in the console.
I struggled with this as well. I end up doing something like this:
Problem for me was that md-autocomplete need the result to be an array, where the response from $http is an object. I hope this help someone else!!
Finally I have done it. Here is the solution.
You can use Angularjs promises to get data from $http call.
Use $apply to kick a digest cycle yourself, it will update the view.