我有用户列表,我会向用户显示。用户有未完成的消息而没有写入用餐列表,我想在主http.get请求中制作http.get以获取所需信息,但我的问题是它异步工作,因此未读消息和周值不会改变。我该怎么做。提前致谢
$http.get('http://example.com/users').success(function(response){
var data=response;
if(data==null || data.length<=0){
return;
}
var date2 = new Date();
var users=[];
for(var i=0;i<data.length;i++){
var diffDays=0;
if(data[i].uyelikBaslangicTarihi=="0"){
continue;
}
var from = data[i].uyelikBaslangicTarihi.substring(0,10).split("/");
var date1 = new Date(from[2], (from[1] - 1), from[0]);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
if(diffDays>28 || diffDays<0){
continue;
}
var week=diffDays/7;
week=parseInt(week);
if(diffDays/7>0)
week++;
if(week!=0)
week--;
var unloadweek=week+1;
var unreadmessage=0;
$http.get('http://example.com/menus?userId=' + data[i]._id + '&haftaSayisi=' + week).success(function(response){
if(response==null || response.length==0){unloadweek= "";}
else{unloadweek= week;}
});
$http.get('http://example.com/sorus?userId=' + data[i]._id + '&okundu=0').success(function(response){
console.log(response);
if(response==null){ unreadmessage= 0;}
else{unreadmessage = response.length;}
});
var color="white"
var gender='mars'
if(data[i].cinsiyet=="Kadın")
gender='venus';
var user={
id:data[i]._id,
name:data[i].ad,
gender:gender,
weight:data[i].kilo,
height:data[i].boy,
birthday:data[i].dogum_tarihi,
email:data[i].email,
src:"http://example.com/images/"+data[i]._id+".jpeg",
color:color,
week:unloadweek,
unreadmessage:unreadmessage
}
if(data[i].uyelikBaslangicTarihi!="" && data[i].uyelikBaslangicTarihi!="0" && data[i].rol!="admin")
users.push(user);
}
$scope.customers=users;
})
.error(function(err){
alert("hata");
}
)
答案 0 :(得分:0)
您需要正确链接您的http请求和承诺。请参阅文档(见下文)。
另外,请尊重该上下文中变量的范围 -
unloadweek
变量在get之前定义,然后在下一个$ http.get resolve函数中使用 - 以及for循环中的所有变量。
不确定您的代码结构是否也存在问题:)建议改进之处; - )
https://docs.angularjs.org/api/ng/service/ $ HTTP GET#