Ich habe eine Funktion an Angular App-Modul durch "app.factory", die bestimmte http-Anforderungen in es hat. Diese Funktion wird im Controller aufgerufen, und die zurückgegebenen Daten von der Funktion werden einer Bereichsvariable im Controller zugewiesen.Schwierigkeiten beim Aufrufen der Funktion in angular js
Das Problem besteht darin, dass der Controller zuerst ausgeführt wird und dann die HTTP-Anfragen in der Funktion ausgeführt werden, aufgrund derer die Daten, die von der HTTP-Anfrage kommen, nicht in der Scope-Variablen erfasst werden können. Wie kann das korrigiert werden?
function resourceapp.factory('dataService', function($http){
var data = {};
data.EnterprisePrograms = [];
data.Resources=[];
data.Skills=[];
data.Allocations=[];
Execution = function($http) {
$http.get('http://localhost:8080/api/enterpriseProgram')
.then(function(resources) {
data.EnterprisePrograms=resources.data;
});
$http.get('http://localhost:8080/api/resource')
.then(function(resources) {
data.Resources=resources.data;
});
$http.get('http://localhost:8080/api/skill')
.then(function(resources) {
data.Skills=resources.data;
});
$http.get('http://localhost:8080/api/allocation')
.then(function(allocation) {
data.Allocations = allocation.data;
});
}
return data;
});
Controller
resourceapp.controller('AllocationList', function($scope, dataService) {
$scope.allocationList = dataService.Allocations;
});
wird nicht funktionieren, seit $ http ist async .. – Founded1898
@ Founded1898: Sie wahrscheinlich richtig, ich aktualisierte die Antwort. – anoop
Es füllt immer noch nicht die $ scope.allocationList aufgrund der Ausführungsreihenfolge @anoop – Sakky