简体   繁体   English

更改数组角度 11 时 UI 闪烁

[英]UI flicker when array is changed angular 11

I am contacting an array on value updated by the server, but the UI template flicker on concat.. what should be done to avoid such a situation.我正在联系一个由服务器更新的值的数组,但 UI 模板在 concat 上闪烁。应该做些什么来避免这种情况。

   @Input('companies')   set setCompanyArray(companies) {
     this.showNotFound = false;> 
     if (!companies) {
       return;
     }
     companies.map((company) => {
       company['searchFilter'] = this.lastSearchedText;
    });
    if (!this.companies) {
       this.companies = companies;
       return;
     }
     this.companies = this.companies.concat(companies);

Each time you assign a new value to companies variable, you will see flickering because Angular is rendering the whole list again.每次为公司变量分配新值时,您都会看到闪烁,因为 Angular 再次呈现整个列表。 You should add trackByFn to tell Angular whether to rerender the whole list or just new items.你应该添加 trackByFn 来告诉 Angular 是重新渲染整个列表还是只重新渲染新项目。

 <li *ngFor="let company of companies;trackBy: trackByFn"></li>

  trackByFn(index, company: Company) {

    return company.id // or whatever is an unique identificator for a company
  }

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

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