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);
});
}]);
It's returning a promise, and you need to handle it this way
In your controller add this:
getSomething.getBla().then(function(response) {
console.log(response.data)
});
for more information: 'http://andyshora.com/promises-angularjs-explained-as-cartoon.html'
This worked in my case: result.$$state.value.
Sample Code: Controller:
promise = getConfigService.getAccount(protocol+hostName+port);
promise
.then(
function (result) {
var baseUrlTemp = result.$$state.value.baseURL;
console.log("BaseURL:"+baseURLTemp)
}
Ouput: BaseURL: www.oneindia.com