放了微调,而$ http.get从angularjs的RESTful API获取数据

问题描述 投票:1回答:2

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

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

这是工作的罚款和足够快。但是,如果有很多项目获取的?我想提出一个微调当数据获取。

我怎样才能做到这一点?

javascript angularjs angularjs-http
2个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.