Ecco come si può fare:
app.factory('autoCompleteDataService', ['$http', function($http) {
return {
getSource: function() {
return function(request, response) {
$http.get(url).success(function(data) {
response(data);
});
}
}
}
}]);
Tuttavia, se si desidera scaricare tutti i dati prima e permettere al widget di completamento automatico per la ricerca dei dati sul lato client, questo è un esempio di come si può fare:
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);
});
}
}
}]);
fatto e ora ho "TypeError: this.source non è una funzione" –
Può si imposta un esempio, allora, visto che non so come si utilizza il servizio di $ hhtp – asgoth
Mi dispiace di aver avuto un errore di battitura, ho risolto e ora non ci sono errori, ma anche nessun ritorno di dati. io uso il servizio $ http esattamente come nella tua risposta sopra. –