Slow rendering of template using angular2 *ngFor

how can i increase the performance when rendering a template multiple times using ngFor? I have a situation where i need to show the same template based upon the count.I am doing this using *ngFor. The template loads correctly but i am worried about the performance.Because I need to repeatedly show the same content for a few thousand times,then the performance will be slow after the template is loaded.I have made a plunker demo here http://plnkr.co/edit/qTj4SVRnFD6N15PZ1GWC?p=preview . please Increase the count of formgroups in formarray with a difference of 1000 then the system will be slow or breaks down. This is how i create the formarray,

       for(var i=0;i<100;i++){
  let dummyFormGroup=this.fb.group({
    'name':[''],
    'place':['']
  })
  this.dummyFormArray.push(dummyFormGroup);
}

And i render the controls in formarray using ngFor like this

  <div *ngFor="let formgroup of dummyFormArray.controls"   style="display:flex;flex-direction:row">
<p>
  <label>Name:</label>
  <input type="text" [formControl]="formgroup.controls['name']" />
</p>
<p>
<label>Place:</label>
<input type="text" [formControl]="formgroup.controls['place']" />
</p>

Can somebody please suggest me an approach where i can increase the performance once the template is loaded? Because i am ok with waiting for the template to load if it needs to render thousands of records.But once the template is ready i want the system to be fast in this case using *ngFor. Somebody please help me in solving this issue. Thanks !


By default angular checks for change detection in every component in the tree.

So for example if you bind key up, to your input components, things are going to be messy pretty fast.

More on change detection here : https://stackoverflow.com/a/39802466/4299560


You could also improve the performance by only rendering the visible templates. Let's say that you start rendering 10 templates, and when you start scrolling (to the bottom of the page), you render more. The web-browser takes some time to render all the DOM-elements. Rendering "just-on-time" is better than rendering "all-at-once". All the data will still be in memory, making sure that sorting, or searching for specific templates may be possible.

链接地址: http://www.djcxy.com/p/93396.html

上一篇: 圆括号是否使指针模板参数无效?

下一篇: 使用angular2 * ngFor缓慢渲染模板