gpt4 book ai didi

javascript - 在 Angularjs 的响应中获取 Promise 数据

转载 作者:行者123 更新时间:2023-12-02 14:18:36 25 4
gpt4 key购买 nike

我有如下一项服务

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

};

下面的 Controller 我用来获取数据

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

在控制台中得到这样的响应 Image for console data reference

在此之后,我尝试获取 Promise 对象内的数据,但无法获取该数据。

最佳答案

getAllSurveyService.surveyList() 返回一个 promise ,因此您需要获取如下数据(在您的 Controller 中):

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

关于javascript - 在 Angularjs 的响应中获取 Promise 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38844509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com