我正在使用 AngularJS。我的 Controller 中有一个函数可以从服务器获取 .json
文件,处理数据并将其存储在 $scope
中。
所以基本上这个函数在页面加载时需要最多的时间。
所以我有这样的东西,我的页面已经加载但是我的表(用 ng-repeat
填充)没有。加载表需要几秒钟。在加载表格时,我想像这样添加进度圈:
在 AngularJS 中什么是合适的方法?
这是一个 Controller 演示:
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;
});
});
});
}
最佳答案
在 Controller 中:
$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
在 View 中:
<img src="loading.gif" ng-show="loading" /> // show the loading.gif when `$scope.loading` variable is `true`
关于javascript - 在 AngularJS 中添加进度圈的适当方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26116281/