简体   繁体   English

相关模型值更改后,Angular 不会重新渲染视图

[英]Angular not re-render view after relate model value change

I'm make simple angular app I have 2 model below我正在制作简单的角度应用程序,下面有 2 个模型

class Customer implements ICustomer
{
    id?: number | null | undefined;
    name: string | null;
    addr: string | null;
    tel: string | null;
    postCode: string | null;
    defaultBrand: number | null;
    defaultLogistic: number | null;
    createdOn: string | null;
    updatedOn: string | null;
}

and

class Brand implements IBrand {

  id?: number | undefined;
  name: string = "";
  shortName: string = "";
  imgPath: string = "";
  isEnable?: boolean | undefined = false;
  createdOn: string = "";
  updatedOn: string = "";
}

So relation is Customer has 1 brand (store as brand id)所以关系是客户有 1 个品牌(商店作为品牌 ID)

then in customer management view I have table that show brand's name and I use ngBootstrap modal for update view然后在客户管理视图中,我有显示品牌名称的表,我使用 ngBootstrap 模式进行更新视图

So, the problem is after I update brand on modal I found the customer table not updated it's still previous value How I correct this value所以,问题是在我在模态上更新品牌后我发现客户表没有更新它仍然是以前的值我如何更正这个值

Here some my customer-row.component.ts这里有一些我的customer-row.component.ts

  ngOnInit(): void {
    this.setBrand();
    this.setLogistic();
  }

  setBrand(): void {
    this.brand = this.brandService.brandList.find(x => x.id === this.item.defaultBrand);
  }

  setLogistic(): void {
    this.logistic = this.logisticService.LogicticList.find(x => x.id === this.item.defaultLogistic);
  }

  onEditClick(): void {
    this.modalRef = this.modal.open(CustomerModalUpdateComponent, {
      animation: true,
      size: 'xl',
      backdrop: 'static'
    });
    this.modalRef.componentInstance.title = `${this.item.name}`;
    this.modalRef.componentInstance.item = this.item;
    this.modalRef.closed.subscribe((result:any) => {
      this.setBrand();
      this.setLogistic();
      this.changeDetectRef.detectChanges();
    });
  }

and here some my customer-row.component.html这里有一些我的customer-row.component.html

 <td class="text-center">
    {{ this.brand?.name }}
  </td>
  <td class="text-center">
    {{ this.logistic?.name }}
  </td>

This may not solve the problem but try assigning the variables to new locations in memory so change detection takes place.这可能无法解决问题,但尝试将变量分配到内存中的新位置,以便进行更改检测。

Try this:试试这个:

setBrand(): void {
    this.brand = { ...this.brandService.brandList.find(x => x.id === this.item.defaultBrand) };
  }

  setLogistic(): void {
    this.logistic = { ...this.logisticService.LogicticList.find(x => x.id === this.item.defaultLogistic) };
  }

I solve problem by implement ngOnChanges code below我通过实现下面的 ngOnChanges 代码来解决问题

  ngOnChanges(changes: SimpleChanges): void {
    this.setBrand();
    this.setLogistic();
  }

I'm not sure this is a correct way but it's can solve my problem我不确定这是一个正确的方法,但它可以解决我的问题

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

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