使用角度4键入输入文本非常慢

时间:2018-03-25 09:24:35

标签: javascript html angular typescript

当我输入文字来搜索某些内容时,在文本中显示一个字符非常慢。 问题是什么 ? 我显示了50个产品ngFor如下所示,如果我显示超过50个产品100或150输入文字更慢。 我该怎么做才能解决这个问题?

<div class="width_products  products-animation " *ngFor="let product of productsService.products ; trackBy: $index"  [ngClass]="{ 'width_products_open_menu':productsService.status_menu  }" >
  <span class="each_width_product" >
    <div  class="title_products more_detail_product" (click)="set_router({ path:product['company'].company_name+'/'+product.product_title , data:product.product_id , relative:true })">
      {{product.product_title }}
      <span class="glyphicon glyphicon-chevron-down"></span><br>
      <div class=' glyphicon glyphicon-time'></div> {{product.product_date}}
    </div>
    <div class="image_product_primary " (click)="set_router({ path:product['company'].company_name+'/'+product.product_title , data:product.product_id , relative:true })">
      <img class="image_product" src="../../assets/images/products_image/{{product.product_image}}">
    </div>
    <button  (click)="product.product_in_wishList='true'; productsService.add_wish_list( product )" mat-button class="wish_list  notCloseDropdawnFavorite notCloseDropdawnCard">
      <span class="write_add_wish">{{dataservices.language.add_wishlist}}</span>
      <mat-icon *ngIf="product.product_in_wishList == 'false' " class="notCloseDropdawnFavorite notCloseDropdawnCard">favorite_border</mat-icon>
      <mat-icon  *ngIf="product.product_in_wishList == 'true' " class="hearts_div_hover notCloseDropdawnFavorite notCloseDropdawnCard">favorite</mat-icon>
    </button>
    <div class="footer_products">
      <span matTooltip="Views!">
        <div class="button_footer_products">
          <span class="glyphicon glyphicon-eye-open icon_eye"></span>
          <div class="both_write ">
            12889
          </div>
        </div>
      </span>
      <span matTooltip="Add to your card"  class="notCloseDropdawnCard notCloseDropdawnFavorite " (click)="product.product_in_cartList='true'; productsService.add_cart_list( product )">
        <div class="button_footer_products">
          <span *ngIf="product.product_in_cartList=='false'" class="glyphicon glyphicon-plus icon_eye notCloseDropdawnCard notCloseDropdawnFavorite" ></span>
          <span *ngIf="product.product_in_cartList=='true'" class="glyphicon glyphicon-ok icon_eye notCloseDropdawnCard notCloseDropdawnFavorite" ></span>
          <div class="both_write ">
            Cart
          </div>
        </div>
      </span>
      <span matTooltip="See Details!">
        <div (click)="set_router({ path:product['company'].company_name+'/'+product.product_title , data:product.product_id , relative:true })" class="button_footer_products" >
          <span class=" glyphicon glyphicon-option-horizontal icon_eye"></span>
          <div class="both_write ">
            More
          </div>
        </div>
      </span>
    </div>
    <div class="prise_products">
      Price:<del>$2500</del> $3500
    </div>
    <div class="plus_height"></div>
  </span>
</div>

在标题组件中,我有一个输入类型文本,如下所示:

<input type="text" class="kerkim"  name="search" [(ngModel)]="typing_search" placeholder=" 
         {{dataservices.language.searchproducts}}">

2 个答案:

答案 0 :(得分:1)

Debouce效果,例如不要立即进行搜索。

class Coponent {

  private _timeoutId: number;

  //to be called on search text changed
  search(){
    clearTimeout(this._timeoutId);

    this._timeoutId = setTimeout(() => {
      //do search stuff
    }, 500) //play with delay
  }
}

使用搜索关键字缓存prev结果。 当kyeword改变如此[&#34; k&#34;,&#34; ke&#34;,&#34; key&#34;]时,您不需要重新整理整个数组。

class Search {

  private _keywordChanges:string[] = [];
  private _prevFilterResults: any[] = [];
  private _allData: any[] = [];


  search(keyword:string){
    let prevKeyword = this.getPrevKeyword(),
      toBeFiltered: any[];
    if(keyword.match(keyword)){ //if it was "ke" and now it is "key"
      //filter prev results only
      toBeFiltered = this._prevFilterResults;
    } else  {
      //filter prev results or even make cache for keyword
      toBeFiltered = this._allData;
    }

    let results = toBeFiltered.filter(() => {});
    this._prevFilterResults = results;
  }

  private getPrevKeyword(){
    return this._keywordChanges[this._keywordChanges.length - 1];
  }

使用for而不是Array.filter(),在某些情况下它可能会有所帮助。例如,你已经排序数组[&#34; a&#34;,&#34; apple&#34;,&#34; b&#34;,&#34; banana&#34;]和关键字&#34; a& #34;

function search(array:any[], keyword:string) {
  //so
  let results = [];
  for(let i = 0; i < array.length; i++){
    let item = array[i];
    if(item.toString().startsWith(keyword)){
      results.push(item);
    } else {
      break; //as b and banana left
    }
  }
  return results;
}

看一下二进制搜索。 How to implement binary search in JavaScript 和哈希表Hash table runtime complexity (insert, search and delete)

答案 1 :(得分:0)

从我的问题开始:由于许多数据,每个输入字段都很慢。因此我在重新加载数据的位置添加了“ changeDetection:ChangeDetectionStrategy.OnPush”,然后一切正常。

@Component({
  selector: 'app-app-item',

  templateUrl: './app-item.component.html',

  styleUrls: ['./app-item.component.css'],

  changeDetection: ChangeDetectionStrategy.OnPush,

})