我正在使用AngularJS.我的控制器中有一个函数,它从服务器获取.json文件,处理数据并将其存储在$scope中.
所以基本上这个功能在页面加载时需要最大的时间.
所以我有这样的东西,我的页面已加载,但我的表(填充ng-repeat)没有.加载表需要几秒钟.当表正在加载时,我想添加进度循环:
什么是在AngularJS中做到这一点的适当方法?
这是一个控制器演示:
var serverData = $http.get('data/topCarObj.json');
function sortCars(){
angular.forEach($scope.tabArray, function(tab){
serverData.success(function(data){
angular.forEach(data, function(key){
....
return myCarList;
});
});
});
}
解决方法:
在控制器中:
$scope.getData = function() { // wrap a data getting code in a function
$scope.loading = true; // define a variable that indicate the loading status // true here
$http.get('path_to_json').success(function (data) { // get the data
$scope.loading = false; // loading is finished so loading = false
})
.error(function (data, status) {
$scope.loading = false; // loading = false when there is a error
});
}
$scope.getData(); // call the function to get data
在视图中:
<img src="loading.gif" ng-show="loading" /> // show the loading.gif when `$scope.loading` variable is `true`