我正在开发一个应用程序,用来过滤和整理两个json文件中的数据。该应用程序将有两个表,使用ngRepeat myData对这些数据进行比较和对比。到目前为止,顶层表已经在请求一个json文件:
app.controller('tableTopController', function ($scope, $http) {
$http.get('first.json').success(function(response){
$scope.myData = response; });下面的表应该读取我的第二个json文件: second.json中的数据。
发布于 2015-08-26 17:43:05
如果您希望在获得第二个文件之前等待第一个调用完成,并且希望在进行比较和对比之前确保加载了所有内容:
app.controller('theController', function ($scope, $http) {
$http.get('first.json').success(function(response){
$scope.firstData = response;
$http.get('second.json').success(function(response1){
$scope.secondData = response1;
//add any other logic you need to do here to compare & contrast
//or add functions to $scope and call those functions from gui
});
});
});或者,按顺序调用它们,但是您需要确保在加载两种方法之前,您的比较和对比都不能启动:
app.controller('theController', function ($scope, $http) {
$http.get('first.json').success(function(response){
$scope.firstData = response;
});
$http.get('second.json').success(function(response1){
$scope.secondData = response1;
});
//add any other logic you need in functions here to compare & contrast
//and add those functions to $scope and call those functions from gui
//only enabling once both firstData and secondData have content
});发布于 2015-08-26 17:51:42
尝试使用$q.all()解决两种承诺,并在两者都成功时执行回调函数。有关更多信息,请参见docs。
var promises = [];
promises.push(getFirstJson());
promises.push(getSecondJson());
$q.all(promises).then(function (results) {
var firstJson = results[0];
var secondJson = results[1];
});
function getFirstJson() {
return $http.get(...);
}
function getSecondJson() {
return $http.get(...);
}https://stackoverflow.com/questions/32233087
复制相似问题