将延迟 pr 计时器添加到 Angular .get() 以测试延迟

Adding a delay pr timer to an Angular .get() to test latency

我正在尝试为 Angular .get() 请求添加延迟,以便模拟延迟或慢速网络。我想测试一下定时器动画是如何显示的等等

有谁知道我怎样才能做到这一点?

这是我的 .get()

的示例
 $scope.loading = true;


    // get all the portfolios and the products
    //http.get('scripts/json/sample-products.json')
    $http.get('http://www.website.com/Pricing/GetProductCatalogue')
        .then(function (allProducts) {
            $scope.listOfProducts = allProducts.data.Connectivity;
        })
        .finally(function() {
            $scope.loading = false;
        });

范围加载将是 div 我用来包含加载动画和消息的范围。

你可以在 succes 函数中添加超时(使用 $timeoutwindow.setTimeout),如下所示:

$scope.loading = true;


    // get all the portfolios and the products
    //http.get('scripts/json/sample-products.json')
    $http.get('http://www.website.com/Pricing/GetProductCatalogue')
        .then(function (allProducts) {
            $timeout(function(){
                $scope.listOfProducts = allProducts.data.Connectivity;
            },10000)

        })
        .finally(function() {
            $scope.loading = false;
        });

使用$timeout

$scope.loading = true;

var getFunc = function(){
    $http.get('http://www.website.com/Pricing/GetProductCatalogue')
    .then(function (allProducts) {
        $scope.listOfProducts = allProducts.data.Connectivity;
    })
    .finally(function() {
        $scope.loading = false;
    });
}

$timeout(getFunc, 1000);

并且不要忘记将 $timeout 注入您的控制器