Angular - Pass data to parent component

前端 未结 2 1522
情话喂你
情话喂你 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条回答
  • 2020-12-20 06:59

    You should try using services to pass values from one component to another. https://www.youtube.com/watch?v=69VeYoKzL6I

    0 讨论(0)
  • 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<any> = new EventEmitter<any>();
    

    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:

    <app-child-component-selector (updateObjDataEvent)="updateObj($event)">
    </app-child-component-selector>
    

    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.

    0 讨论(0)
提交回复
热议问题