Skip to content Skip to sidebar Skip to footer

Angular 2 Performance Issue (rendering)

I'm doing a project in Angular 2 and I'm getting an issue that makes my pages lose a lot of performance. So, I have a table and everytime I scroll (up or down) it keeps calling one

Solution 1:

Don't call the entriesToShow() function from your template! It cause's the function to be called every change detection! Instead you should store the data in a variable in your component and the ngFor should iterate over him. .component.ts

        entries:Array<any>;

        ngOnInit(){
           this.entries=this.entriesToShow();
        }
        entriesToShow(): Array<any>{
            let entries = [];
            let i = 0;
            if(Number(this.startingEntry)+Number(this.numOfEntries) <this.totalEntries)
                this.lastShowingEntry = Number(this.startingEntry)+Number(this.numOfEntries);
            elsethis.lastShowingEntry = this.totalEntries;

            if(this.dataTable != null && this.dataTable.aaData != null){
                for(i = this.startingEntry; i< this.lastShowingEntry; i++){
                    entries.push(this.dataTable.aaData[i]);
                }
                this.lastShowingEntry = i;
                return entries;
            }elsereturnnull;
        }

.html

<div class="col-md-12" *ngIf="isDataTableAvailable">
     <tableid="table-EDIImports"class="table table-bordered display"cellspacing="0"><thead><tr><th><strong>{{ 'POINT_OF_SALE' | translate }}</strong></th>

                    (more fields)

                    <th *ngIf="mode=='EDIT'"></th></tr><tr *ngFor="let obj of entries" [ngSwitch]="obj.Status"><th>{{ obj.PointOfSell }}</th><th *ngIf="obj.LineID == editRowId && selectedField == 'NIF' && mode=='EDIT'"><inputtype="text" [(ngModel)]="valueToSave"value="{{ obj.NIF }}"></th>
                        (more fields)
              </tr></thead><tbody></tbody></table></div>

Post a Comment for "Angular 2 Performance Issue (rendering)"