正如您在这个问题中提供的代码中看到的那样,我尝试遍历对象的属性。所有属性都是空对象!我还有另一个对象,我在其中存储了几个休息调用(ngResource Stuff,这对这个问题并不重要)。
在这里,您可以看到存储在$scope.restCalls中的“Rest-Calls” 。
$scope.restCalls = [
{
resource1: RestService.call1,
resource2: RestService.call2,
resource3: RestService.call3
},
{
resource4: RestService.call4,
resource5: RestService.call5,
resource6: RestService.call6
},
{
resource7: RestService.call7,
resource8: RestService.call8,
resource9: RestService.call9
}
];
$scope.data表示每个选项卡的数据。此数组中的每个对象都保存选项卡的数据。所有资源都初始化为空,如果用户更改页面,资源将存储在这里。
$scope.data = [
{
resource1: {},
resource2: {},
resource3: {}
},
{
resource4: {},
resource5: {},
resource6: {}
},
{
resource4: {},
resource5: {},
resource6: {}
}
];
到目前为止,一切都很好。我保证电话工作正常。在我的应用程序中有多个选项卡,所以我想尝试实现一些延迟加载:D
因此我实现了一个函数:(索引在html中定义,只是0到2之间的一个数字)
<uib-tab heading="Tab1" select="tabChange(0)">
... HERE I have some tables which access the $scope.data[0] data
</uib-tab>
<uib-tab heading="Tab2" select="tabChange(1)">
... HERE I have some tables which access the $scope.data[1] data
</uib-tab>
<uib-tab heading="Tab3" select="tabChange(2)">
... HERE I have some tables which access the $scope.data[2] data
</uib-tab>
在这里你可以看到函数:
$scope.tabChange = function (index) {
for (var propertyName in $scope.data[index]) {
$scope.restCalls[index][propertyName]().$promise.then(function (data) {
$scope.data[index][propertyName] = data;
});
}
};
现在让我们来看看问题描述:
- 当我更改选项卡时,函数 tabChange被触发。
- 每个 Restcall 都会被正确触发
结果只会存储到$scope.data[index]的错误属性中。它始终是最后一个属性名。因此,例如,我更改为 tab2(索引 1)。 $scope.data最终会像这样:
$scope.data = [
{
resource1: {},
resource2: {},
resource3: {}
},
{
resource4: {},
resource5: {},
resource6: RESULT OBJECT OF THE LAST REST CALL!
},
{
resource7: {},
resource8: {},
resource9: {}
}
];
我认为属性名在then 函数中不可用。但我不知道如何将名称输入此函数。