Angular - Pass data to parent component

前端 未结 2 1541
情话喂你
情话喂你 2020-12-20 06:42

I have in my parent component this:

 obj: any = { row1: [], row2: [], total: [], sumTotal: 0 };

I pass obj to child component :

         


        
2条回答
  •  Happy的楠姐
    2020-12-20 07:13

    Inside this child component i have another child component where i change values of array but its not changing on my parent object.

    Data flow between parent and child components is not two-way in Angular. So the changes you make in a child component won't be reflected in the parent component.

    You can use EventEmitter to emit events from child component to parent component.

    Child component:

    In your child component, declare an EventEmitter object.

    @Output() updateObjDataEvent: EventEmitter = new EventEmitter();
    

    Then use emit method from anywhere in the child component to send data to parent component.

    // Update parent component data.
    this.updateObjDataEvent.emit(obj);
    

    Parent component:

    In your parent component template, subscribe to this event:

    
    
    

    Then in your parent component, create updateObj() method to handle data updates from child component.

    updateObj(data) {
      // Do something.
    }
    

    In the updateObj() method, you can update your parent component's array object.

提交回复
热议问题