2016-02-05 88 views
0

我試圖得到以下Angular-Spinner在我的項目工作:角JS加載問題

我試圖把它與http.get調用工作。到目前爲止,我有:在控制器

$scope.loading = true; 
$http.get('js/data/test.json').success(function(results){ 
$scope.section = results.section; 
$scope.loading = false; 

}); 

在視圖:

<span us-spinner="{radius:15, width:4, length: 8}" ng-show="loading"></span> 
<div ng-repeat="post in section" class="inner"></div> 

目前加載顯示速度非常快,我想知道這是使用它的正確方法?在所有數據加載並顯示微調器之前,我怎麼才能稍微延遲它。有任何想法嗎?

回答

1

你可以嘗試添加超時:

$scope.loading = true; 

setTimeout(function() { 
    $http.get('js/data/test.json').success(function(results){ 
     $scope.section = results.section; 
     $scope.loading = false; 
    } 
}, 5000); 

這將試圖檢索從JSON文件中的數據之前,等待5秒鐘。編輯: 使用Angular的$ timeout服務。不要忘記把它包含在你的控制器中! (謝謝Walfrat)

$timeout(function() { 
    $http.get('js/data/test.json').success(function(results){ 
     $scope.section = results.section; 
     $scope.loading = false; 
    } 
}, 5000); 
+1

更好:使用$ timeout的角度。 – Walfrat

+0

當然,沒有真正原因推遲數據加載5秒是最好的。 – deceze

+0

@ Walfrat,這將如何與上面的代碼一起工作。 – rob