【问题标题】:Angular 2 performance issue (rendering)Angular 2 性能问题(渲染)
【发布时间】:2017-07-11 21:22:52
【问题描述】:

我正在使用 Angular 2 做一个项目,但遇到了一个问题,导致我的页面性能下降很多。所以,我有一张桌子,每次我滚动(向上或向下)时,它都会不断调用我的一个函数。就是这个:

.component.ts

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);
    else
        this.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;
    }else
        return null;
}

在我的 html 中,我得到了这样的内容:

<div class="col-md-12" *ngIf="isDataTableAvailable">
     <table id="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 entriesToShow()" [ngSwitch]="obj.Status">
                    <th>{{ obj.PointOfSell }}</th>
                    <th *ngIf="obj.LineID == editRowId && selectedField == 'NIF' && mode=='EDIT'">
                        <input type="text" [(ngModel)]="valueToSave" value="{{ obj.NIF }}">
                     </th>
                        (more fields)
              </tr>
           </thead>
           <tbody></tbody>
       </table>
 </div>

有什么建议可以让我的页面在每次向上/向下滚动时停止调用 entriesToShow()

感谢您的帮助。

编辑:删除了一些额外的代码

【问题讨论】:

标签: html performance angular typescript


【解决方案1】:

不要从您的模板中调用 entriesToShow() 函数!它导致每次更改检测都调用该函数! 相反,您应该将数据存储在组件中的变量中,并且 ngFor 应该遍历他。 .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);
            else
                this.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;
            }else
                return null;
        }

.html

<div class="col-md-12" *ngIf="isDataTableAvailable">
     <table id="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'">
                        <input type="text" [(ngModel)]="valueToSave" value="{{ obj.NIF }}">
                     </th>
                        (more fields)
              </tr>
           </thead>
           <tbody></tbody>
       </table>
 </div>

【讨论】:

  • 嗯...那行不通..我无法以这种方式呈现数据...检查 img:imgur.com/H6qkCIK
  • 为什么取不到数据?你想在图片中向我展示什么?
  • 嗯...页面将访问数据库然后加载数据,当 html 是渲染器时我还没有任何条目,我想那是让表为空
  • 您是否调试过代码以确保 entriesToShow() 不返回 null 或空数组?
  • 好吧,分页和条目数都在那里,如果你看到我有 4334 个条目,所以它不为空...获取条目的方法工作正常,我的问题只是每次我滚动它一遍又一遍地运行该方法
猜你喜欢
  • 2017-08-03
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-14
  • 2015-08-07
  • 2020-05-02
  • 2012-04-09
相关资源
最近更新 更多