为什么可以';ti在javascript中访问对象值
我正在尝试获取和分析数据,但我不知道如何等到上面的每一条指令都完成 这是我的密码:为什么可以';ti在javascript中访问对象值,javascript,angularjs,Javascript,Angularjs,我正在尝试获取和分析数据,但我不知道如何等到上面的每一条指令都完成 这是我的密码: function get_unicodes() { var deferred = $q.defer(); var result = {'seen': [], 'all': []}; var unicode_seen_raw = window.localStorage.getItem('LISTE_CARACTERES').split(" ");
function get_unicodes() {
var deferred = $q.defer();
var result = {'seen': [], 'all': []};
var unicode_seen_raw = window.localStorage.getItem('LISTE_CARACTERES').split(" ");
var unicode_all = window.localStorage.getItem('CAROBJECTIF').split(" ");
for (value in unicode_seen_raw) {
$http({
method: 'post',
url: DataService.URL,
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
data: $httpParamSerializerJQLike({ no_requete: 16, sParam: value })
}).then(function (res, data) {
result['seen'].push(JSON.parse(res['data']['data'])[0]);
});
}
for (value in unicode_all) {
$http({
method: 'post',
url: DataService.URL,
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
data: $httpParamSerializerJQLike({ no_requete: 16, sParam: value })
}).then(function (res, data) {
result['all'].push(JSON.parse(res['data']['data'])[0]);
});
}
console.log(result);
console.log(result['seen']);
deferred.resolve(result);
return deferred.promise;
}
function update_biblio() {
get_unicodes().then(function (res) {
// stuff I want to do with res but can't
}
}
以下是我得到的:
经过一些研究,我发现当时console.log()
被调用result['seen']
的值没有设置。但我不知道如何解决这个问题
我是否应该调用一个函数来等待我的http请求完成,或者它们是一种更好的方法?
$http
是异步的,因此您可以在任何请求完成之前立即解析承诺
您可以使用$q.all()
以及$http
function get_unicodes() {
// array to store all the request promises
var promises = [];
var result = {'seen': [],'all': []};
var unicode_seen_raw = window.localStorage.getItem('LISTE_CARACTERES').split(" ");
var unicode_all = window.localStorage.getItem('CAROBJECTIF').split(" ");
for (value in unicode_seen_raw) {
var req1 = $http(...).then(function(res, data) {
result['seen'].push(JSON.parse(res['data']['data'])[0]);
});
// push this promise to promise array
promises.push(req1);
}
for (value in unicode_all) {
var req2 = $http(...).then(function(res, data) {
result['all'].push(JSON.parse(res['data']['data'])[0]);
});
// push this promise to promise array
promises.push(req2);
}
// return the `$q.all()` promise
return $q.all(promises).then(function() {
// fires after all promises in array are resolved
console.log(result);
console.log(result['seen']);
// return result
return result;
}).catch(function() {
// do something when not all requests succeed
});
}
$http
是异步的,因此您可以在任何请求完成之前立即解析承诺
您可以使用$q.all()
以及$http
function get_unicodes() {
// array to store all the request promises
var promises = [];
var result = {'seen': [],'all': []};
var unicode_seen_raw = window.localStorage.getItem('LISTE_CARACTERES').split(" ");
var unicode_all = window.localStorage.getItem('CAROBJECTIF').split(" ");
for (value in unicode_seen_raw) {
var req1 = $http(...).then(function(res, data) {
result['seen'].push(JSON.parse(res['data']['data'])[0]);
});
// push this promise to promise array
promises.push(req1);
}
for (value in unicode_all) {
var req2 = $http(...).then(function(res, data) {
result['all'].push(JSON.parse(res['data']['data'])[0]);
});
// push this promise to promise array
promises.push(req2);
}
// return the `$q.all()` promise
return $q.all(promises).then(function() {
// fires after all promises in array are resolved
console.log(result);
console.log(result['seen']);
// return result
return result;
}).catch(function() {
// do something when not all requests succeed
});
}