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

时间:2015-02-02 11:50:09

标签: angularjs timer

我正在尝试向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。

2 个答案:

答案 0 :(得分:2)

您可以在成功功能中添加超时($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;
        });

答案 1 :(得分:0)

使用$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

相关问题