繁体   English   中英

Angular 子组件未检测到更改

[英]Angular child component does not detect change

我正在从父组件更新产品对象。 不知何故,子组件不会检测到更改并且不会更新它。

产品.ts

export interface Product { 
    productId: number;
    productName: string;
    lastUpdated: string;
    orders: Array<Order>
}

订单.ts:

export interface Order { 
    orderId: number;
    payments: Array<Payment>
}

付款.ts:

export interface Payment { 
    paymentId: number;
    amount: Array<number>
}

父组件.html

<product-details [(product)]="product1" (refreshProduct)="refreshProduct()"></product-details>

父组件.ts

product1: Product = null;

refreshProduct() {
      this.sub = this.productService.getTheLatestOrders().subscribe(
        (data) => {
          this.product1.lastUpdated = data.lastUpdated;
          this.product1.orders.forEach(order => {
            let latestOrderData = data.orders.find(d => d.orderId == order.orderId);
            if(latestOrderData) {
              order.payments = latestOrderData.payments;
            }
          });
          // this.product1 = JSON.parse(JSON.stringify(this.product1)); --> It works if I add this
      });
    }
  }

product-details.component.html(子组件)

<button id="refresh" name="refresh" (click)="refresh()" />
Last Updated : {{product.lastUpdated}}

<ng-container *ngFor="let order of product.orders">
      <ng-container *ngFor="let payment of order.payments">
             {{payment.date}} - {{payment.amount}} 
      </ng-container>
</ng-container>

product-details.component.ts(子组件)

@Input('product') product: Product;
@Output() refreshProduct = new EventEmitter<any>();

refresh() {
  this.refreshProduct.emit();
}

我试图明确声明changeDetection: ChangeDetectionStrategy.Default但没有运气。

如代码中所述,如果我添加JSON.parse(JSON.stringify(this.product1)); 那么它的工作原理。 所以似乎我需要创建一个新对象,以便更改检测工作。 我想我可以使用扩展运算符 (object.assign) 来确保它创建一个新对象。 但是,我不确定如何使用传播操作更新 refreshProduct() 方法中的产品。

我认为代码将如下所示:

this.product1 = {...this.product1, 
            lastUpdated: data.lastUpdated,
            orders: .... // --> Not sure how to loop through orders and update the payments

          };

编辑:我想我设法做到了。

this.product1 = {...this.product1, 
            lastUpdated: data.lastUpdated,
            orders: this.product1.orders.map((order) => {
                     let updatedOrder = data.orders.find(o => o.orderId == order.orderId);  
                     return {...order, order.payments: updateOrder.payments};

                    })
          };

请让我知道是否有更好的解决方案。

尝试这样做:

refreshProduct() {
      let temp: Product = {};
      this.sub = this.productService.getTheLatestOrders().subscribe(
        (data) => {
          temp.lastUpdated = data.lastUpdated;
          temp.orders.forEach(order => {
            let latestOrderData = data.orders.find(d => d.orderId == order.orderId);
            if(latestOrderData) {
              order.payments = latestOrderData.payments;
            }
          });
          this.product1 = temp;
      });
    }
  }

这样每次它都会将更新的值分配给产品。

暂无
暂无

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

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