2014-12-02 45 views
1

我取一个数据,在我的控制器此功能的微调:

var fetchStoreItems = function() { 
    return $http.get('/enterprises/_store').then(function (response) { 
     $scope.items = response.data; 
    }, function (errResponse) { 
     console.error("Error while fetching data") 
    }) 
}; 

这是工作的罚款和足够快。但是如果有很多物品要取?我想在数据被提取时加入微调。

我该怎么做?

回答

3

在你的控制器,

var fetchStoreItems = function() { 
    $scope.loading = true;   //define a `$scope` variable; show loading image when ajax starts 
    return $http.get('/enterprises/_store').then(function (response) { 
     $scope.items = response.data; 
     $scope.loading = false;  //hide loading image when ajax successfully completes 
    }, function (errResponse) { 
     console.error("Error while fetching data"); 
     $scope.loading = false;  //hide loading image when ajax error 
    }) 
}; 

<img src="pathToLoadingImage" ng-show="loading" /> // show the loading image according to `$scope.loading` 
1

如果你想使用它的一些,其余的API调用,kalhano的回答运作良好。 但是,如果你想要所有的角度http调用微调,你可以考虑使用拦截器。

请检查此链接为: Http call docs with interceptors explained