How to get data from {$$state: Object}?

后端 未结 3 1377
南方客
南方客 2021-01-19 08:11

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

3条回答
  •  不知归路
    2021-01-19 08:52

    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);
       });
    
    }]);
    

提交回复
热议问题