简体   繁体   English

将对象数组用作子角度分量的模型,并注意更改

[英]Use array of objects as model on child angular component and watch for changes

I have a component that displays a list of items (objects). 我有一个显示项目(对象)列表的组件。 I want to create another component that receives this list as its model (or as a parameter) and creates a dashboard with this data. 我想创建另一个组件,以将该列表作为其模型(或作为参数)接收并使用此数据创建仪表板。 For example, I have a list of products, with quantity and price per unit, and want to display on the child component the total price. 例如,我有一个产品列表,其中包含数量和单价,并希望在子组件上显示总价。

By creating the component and passing the list to the child the total price is displayed correctly, however, when new items are added to the list, or when the quantity/price of an item is changed inside the list, my child component does not get triggered and does not updates it self. 通过创建组件并将列表传递给子组件,可以正确显示总价,但是,当将新项目添加到列表中时,或者在列表内更改了项目的数量/价格时,我的子组件无法得到触发并且不会自我更新。

I already tried to use ControlValueAccessor and OnChanges 我已经尝试使用ControlValueAccessorOnChanges

Code on parent 父代码

<result [(ngModel)]="productList" [test]="armorList"></result>

Code on child - I know that using model and input might not be correct, I just tried both , sot sure which would be better. 关于子代的代码-我知道使用模型和输入可能不正确,我只是尝试了两种,所以肯定会更好。

import { Component, Input, OnChanges, Output, EventEmitter, forwardRef, SimpleChanges } from "@angular/core";
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
import { Armor, Material } from "../armor-list.component";
import * as _ from "lodash";


@Component({
    selector: 'result',
    templateUrl: './result.component.html',
    styleUrls: ['./result.component.css'],
    providers: [
        {
            provide: NG_VALUE_ACCESSOR,
            useExisting: forwardRef(() => ResultComponent),
            multi: true
        }
    ]
})
export class ResultComponent implements ControlValueAccessor, OnChanges {

    @Input() test: Product[];

    ngOnChanges(changes: SimpleChanges): void {
        console.log(changes);
    }

    writeValue(obj: any): void {
        if (obj !== undefined && obj !== null) {
           //logic to calculate the price
        }
    }
    registerOnChange(fn: any): void {
    }
    registerOnTouched(fn: any): void {
    }
}

Thanks 谢谢

Angular detects changes in array and object by reference (not by value). Angular通过引用(而不是通过值)检测数组和对象的变化。 Since you have just added an item to array, its reference stills remain same. 由于您刚刚将项目添加到数组,因此其引用静止图像保持不变。

So to detect changes in array, you need to adopt either of the following strategies. 因此,要检测阵列中的更改,您需要采用以下两种策略之一。

  1. Use ngDoCheck lifecycle hook. 使用ngDoCheck生命周期挂钩。 See https://angular.io/guide/lifecycle-hooks . 参见https://angular.io/guide/lifecycle-hooks
  2. Use slice method on input array. 在输入数组上使用切片方法。 Whenever you are adding items to array, modify input array as let input = originalArray.slice(0). 每当将项目添加到数组时,将输入数组修改为let input = originalArray.slice(0)。 This will give you a new reference address which angular can detect. 这将为您提供一个角度可以检测到的新参考地址。

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

相关问题 观察组件变量 Angular 5 的变化 - Watch changes in component variable Angular 5 如何在 angular 中将对象数组从子组件传递到父组件 - How to pass Array of Objects from Child Component to Parent Component in angular angular:对象数组中的$ watch对象 - angular: $watch object in array of objects 如何在模型中使用子组件在Angular 6中打开另一个组件? - How to use Child Component inside the model to open an Another Component in Angular 6? Angular2 @Input:将对象数组从父组件传递到子组件 - Angular2 @Input : Passing an objects array from parent to child component 如何将异步数据作为对象数组传递给子组件-angular 6 - How to pass async data as a Array of Objects to child component - angular 6 如何根据基础组件的变化检测子组件的变化 - Angular - How to detect changes in child component with respect to changes in the base component - Angular 我们如何将输入模型类中的每个更改从 Angular 13 中的子组件推送到父组件 - How can we push each changes in input model class to parent component from child in Angular 13 Angular 2监视组件属性,用于没有@Input或@Output的更改 - Angular 2 watch component property for changes without @Input or @Output 数据源更改时,Angular 2子组件未更新 - Angular 2 child component not updating when datasource changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM