I am new to angular 1, and I have problem with my code:
var app = angular.module("Football", []);
app.factory("competitions", ['$http', function($http) {
return $http.get("json/competitions.json")
.success(function(response) {
var data = {
response: response,
teams: []
};
for (var i = 0; i < response.length; i++) {
$http.get("json/teams.json")
.success(function(response2) {
data.teams.push(response2);
return data
})
.error(function(err) {
return err;
});
}
})
.error(function(err) {
return err;
});
}]);
app.controller('MainController', ['$scope', "competitions", function($scope, competitions) {
competitions.success(function(data) {
$scope.competitions = data;
});
}]);
I want to pass data from competitions factory to $scope.competitions in MainController. After last iteration of for loop data variable should be passed to the controller. I know this code is wrong, beacuse it passes only response to controller, but I don't know how to fix it. Could someone help me?