Kishan
Kishan

Reputation: 388

Getting Promise data in the response in Angularjs

I have one service like below

getServices.js

angular
    .module('adminsuite')
    .service('getAllSurveyService', getAllSurveyService);
    getAllSurveyService.$inject = ['$http', '$cookieStore', '$rootScope', '$timeout', '$q'];
     function getAllSurveyService($http,$cookieStore,$rootScope,$timeout,$q){
        var deferred = $q.defer();
        var session = $cookieStore.get('globals');
        var data = {
            sessionId : session.currentUser.session
        }
        var surveyData = {
            'Data' : JSON.stringify(data)
        }
        var requestData = JSON.stringify(surveyData)
        //console.log(requestData);

        this.surveyList = function () {
            //return session;
            return $http.post('http://apidev.1pt.mobi/V3.0/api/Survey/Surveys',  requestData, {headers: { 'Content-Type': 'application/json'}})
            .then(function(response){
                        var sendData = deferred.resolve(response.data);
                        //console.log(JSON.stringify(response.data));
                        return response.data;
                    }, function(error){
                        return error;
                    });
        }

    };

Below controller i am using for fetching the data

controller.js

angular.module("adminsuite").controller("surveyController",['getAllSurveyService', '$scope', function(getAllSurveyService, $scope){
    $scope.header = "Header";
    $scope.allSurveys = getAllSurveyService.surveyList();
    console.log($scope.allSurveys);
    //console.log($scope.getSession);
 }]);

In the console am getting the response like this Image for console data reference

After this am trying to get data inside the promise object but am unable to fetch that.

Upvotes: 1

Views: 3003

Answers (1)

Alon Eitan
Alon Eitan

Reputation: 12025

getAllSurveyService.surveyList() return a promise, so you need to get the data like this (In your controller):

getAllSurveyService.surveyList().then(
    function( data ) {
        $scope.allSurveys = data;
        console.log($scope.allSurveys);
    }
);

Upvotes: 3

Related Questions