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

后端 未结 3 1376
南方客
南方客 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);
       });
    
    }]);
    
    0 讨论(0)
  • 2021-01-19 08:57

    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'

    0 讨论(0)
  • 2021-01-19 08:58

    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

    0 讨论(0)
提交回复
热议问题