Javascript 表(ng repeat)不能正确加载多个ajax调用
我有一个表,除了一个由一个后端调用填充的列之外,它获取所有列。对服务器进行单独调用以获取最后一列的数据。我注意到的是,有时这些数据会正确地显示在表上,有时则不会。可能是因为第二次ajax调用的时间。想知道什么是解决这个问题的正确方法。这是我的 HTML:Javascript 表(ng repeat)不能正确加载多个ajax调用,javascript,html,css,angularjs,Javascript,Html,Css,Angularjs,我有一个表,除了一个由一个后端调用填充的列之外,它获取所有列。对服务器进行单独调用以获取最后一列的数据。我注意到的是,有时这些数据会正确地显示在表上,有时则不会。可能是因为第二次ajax调用的时间。想知道什么是解决这个问题的正确方法。这是我的 HTML: 最后一列图标检查没有一致地应用活动css类。它有时在重新加载页面后工作,有时不工作。我在这里肯定做了一些不正确的事情…我尝试在$timeout和$scope中包装此.loadColors。$apply,但两者似乎都无法解决此问题。由于您的输出取
最后一列图标检查没有一致地应用活动css类。它有时在重新加载页面后工作,有时不工作。我在这里肯定做了一些不正确的事情…我尝试在$timeout和$scope中包装此.loadColors。$apply,但两者似乎都无法解决此问题。由于您的输出取决于两个不同调用的结果的响应,因此您需要将它们链接起来,以便在两个调用完成后获得结果
loadItems() {
myService.getItems().then( (response) => {
this.items = response;
myService.getColors().then( (colorResponse) => {
_.forEach(colorResponse, (val, key) => {
_.forEach(this.items, function(item) {
if (item.id === key) {
item.colors = val;
}
});
});
});
});
}
这应该可以解决问题
编辑我修改了代码,可能是因为项目分配得更早。在修改后的代码中,我将其移动到最后一步
loadItems() {
myService.getItems().then( (response) => {
var items = response;
myService.getColors().then( (colorResponse) => {
_.forEach(colorResponse, (val, key) => {
_.forEach(items, function(item) {
if (item.id === key) {
item.colors = val;
}
});
});
this.items = items; //Assigning it to items after we have set the colors.
});
});
}
我迷路了,这是angularJs还是angularJs?我不知道有什么区别,直到你提到它:这是angularJs 1.xcan,你可以用一秒钟来链接它。然后代替这个。LoadColor?不幸的是,这似乎无法解决问题。我重新加载了5次页面,其中只有4次列得到了正确更新。@Prabhu您可以检查第二个版本是否解决了您的问题。
loadItems() {
myService.getItems().then( (response) => {
this.items = response;
myService.getColors().then( (colorResponse) => {
_.forEach(colorResponse, (val, key) => {
_.forEach(this.items, function(item) {
if (item.id === key) {
item.colors = val;
}
});
});
});
});
}
loadItems() {
myService.getItems().then( (response) => {
var items = response;
myService.getColors().then( (colorResponse) => {
_.forEach(colorResponse, (val, key) => {
_.forEach(items, function(item) {
if (item.id === key) {
item.colors = val;
}
});
});
this.items = items; //Assigning it to items after we have set the colors.
});
});
}