I try to get data from database using factory. I have a problem, because I don\'t know how to get data from object which was returned from factory. This is what I received
You should pass a callback function from controller and execute it in your factory once the AJAX response is received. You can update your code to following.
Factory
application.factory('Database', ['$http', function($http) {
var databaseFactory = {};
databaseFactory.get = function(query, callback) {
return $http.get("getData.php", {params: {'query': query}}, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
.then(function (response) {
databaseFactory.returnedData = response.data;
callback(databaseFactory.returnedData);
})
}
return databaseFactory;
}]);
Controller
registration.controller('RegistrationController', ['$scope', 'Database', function($scope, Database) {
Database.get("SELECT * FROM `group`", function(resp){
$scope.posts = resp;
console.log($scope.posts);
});
}]);