gpt4 book ai didi

javascript - 如何从 Controller 内访问服务返回的对象

转载 作者:行者123 更新时间:2023-12-03 05:26:31 25 4
gpt4 key购买 nike

我有一项服务将对象数组返回到 Controller (即控制台)显示以下响应:

Array[0]
0
:
Object
first_name
:
"Emi"
id
:
"255860959988416847"
last_name
:
"Hauritz Seino"
url
:
"https://www.pinterest.com/amusemi/"
__proto__
:
Object
1
:
Object
first_name
:
"mohammed"
id
:
"474848491869398608"
last_name
:
"al-kahtani"
url
:
"https://www.pinterest.com/alkahtani0848/"
__proto__
:
Object
2
:
Object
first_name
:
"❤ T r e n d 2 W e a r ❤ "
id
:
"418553496527224475"
last_name
:
""
url
:
"https://www.pinterest.com/trend2wear/"
__proto__
:
Object
length
:
3
__proto__
:
Array[0]

我需要帮助来理解数组中包含三个对象时如何显示“0”长度,以及如何从 Controller 内访问这些对象。

Controller 代码:

$http.get('json/ConfigData.json').success(function(data) {
$scope.ebayApiURL = data.ebay.BeautyebayPopularItems;
var parampins= 'PinsByBoard';
var paramApiURL='PinsByBoardByTokenAPIURL'
arrpins=data.Pinterest[parampins];
arrapiurl =data.Pinterest[paramApiURL];
console.log(arrapiurl);
console.log(arrpins);
$scope.UrlArr=[];
for(var i=0; i<data.Pinterest[parampins].length ;i++)
{
var PinFullUrl=arrapiurl + arrpins[i] + data.Pinterest.PiNJSONCALLBACKByToken;
console.log(PinFullUrl);
$scope.UrlArr[i]=PinFullUrl;

}
$scope.Creator=[];
$scope.Creator=Comics.LoadDataFromPinteByToken($scope.UrlArr);
console.log($scope.Creator);
})

服务代码:

var deferred = $q.defer();
var urlCalls = [];
var mergedArray =[];
var k=0;
angular.forEach(UrLArray, function(url)
{
urlCalls.push($http.jsonp(url));
});
$q.all(urlCalls)
.then(function(results) {
deferred.resolve(
JSON.stringify(results.length))
console.log(results);
for(i=0;i<results.length;++i)
mergedArray[i]=results[i].data.data.creator;


},
function(errors) {
deferred.reject(errors);
},
function(updates) {
deferred.update(updates);
}

)
return mergedArray;

}

最佳答案

您必须像这样返回服务中的 promise

var deferred = $q.defer();
var urlCalls = [];
var mergedArray =[];
var k=0;
angular.forEach(UrLArray, function(url)
{
urlCalls.push($http.jsonp(url));
});
$q.all(urlCalls)
.then(function(results) {
deferred.resolve(
JSON.stringify(results.length))
console.log(results);
for(i=0;i<results.length;++i)
mergedArray[i]=results[i].data.data.creator;

})
return deferred.promise; // return the promise

}
// and use then within controller
// like : service.getData().then(function(resolved_data){
//console.log(resolved_data)
//})

关于javascript - 如何从 Controller 内访问服务返回的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41128514/

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