简体   繁体   English

部分功能后重新渲染

[英]Rerender after part of function

I have lots of data in an array and want to render all items in it. 我在数组中有很多数据,并希望呈现其中的所有项目。 This may take a while to render and therefore I want to insert a loading animation before the rendering starts. 渲染可能需要一段时间,因此我想在渲染开始之前插入加载动画。

The code looks something like this: 代码看起来像这样:

$scope.foo = function() {
   // Start loading animation
   $loading.start();

   // Load objects into an array. These objects are 
   // rendered by the ng-repeat directive from angular
   $scope.array = data;
}

But when I execute the function foo, first ng-repeat renders all items and afterwards the loading animation starts. 但是当我执行函数foo时,首先ng-repeat渲染所有项目,然后开始加载动画。 How can I achieve a reversed behavior (First the loading animation and then the ng-repeat rendering)? 如何实现反向行为(首先是加载动画,然后是ng-repeat渲染)?

Inject $timeout to the dependencies of the controller and then change the function to: $timeout注入控制器的依赖项,然后将函数更改为:

$scope.foo = function() {
   // Start loading animation
   $loading.start();

   // Load objects into an array. These objects are 
   // rendered by the ng-repeat directive from angular
   $timeout(function() {
       $scope.array = data;
   });
}

The timeout will let the view refresh before you start the data processing 超时将使视图刷新,然后再开始数据处理

You can use $timeout to achieve it. 您可以使用$ timeout来实现它。 In your code set timeout with load duration. 在代码中设置超时和加载持续时间。

$scope.foo = function() {
  // Start loading animation
  $loading.start();

  $timeout(function() {
    // Load objects into an array. These objects are 
    // rendered by the ng-repeat directive from angular
    $scope.array = data;
  }, YOUR_LOAD_TIME);
}

Don't forget to inject $timeout in your controller. 不要忘记在控制器中注入$timeout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM