Calling parent method from child component not working with callback

Sahbaz

I have one situation and i am not sure what is happening, so basically i have two components: parent and child, and my structure looks like this:

in parent component i have some data that is shown on page, and i get that data when page is initialized calling method getPersonData() -> this returns person data from API.

Also in parent component html i have child component where i also need functionality to refresh data in parent component (and to see potential changes reflected on HTML), what i did here is, i passed as @Input param function from parent component and called it from child component when i wanted to refresh page.

parent component method:

    getPersonData() {
        this.personService.getPersonData(id).then((data) => {
          this.personData = data;
          console.log(this.personData)
        });
    }

parrent component html:

<div>
  {{person.firstName}} {{person.lastName}}
  <child-component [getPersonData]="getPersonData" ></child-component>
</div>

And in child component i have something like this:

@Input() getPersonData: () => void;

  savePerson() {
    this.personService.savePersonData(personId, this.personPayload).subscribe(
      () => {
        this.getPersonData();
      });
  }

And now weird thing happens: in console log and network tab i see that API CALL is made, and i see changes there, but my html remains with old data, that is what i do not understand, console log data is not same like html data, console log and everything is in parent component. Can anyone help me to understand what is happening here, what i am missing ? What is main reason html is not refreshed, it looks like personData that is called from parent component is not same parentData that is called with callback function from child component...

Soumya Gangamwar

use @output for know the parent to call get functions after save made from child component, it's like informing parent to do call when required

<child-component [getPersonData]="getPersonData" (getevent)="getPersonData()"></child-component>

child.component

@output getevent = new EventEmitter();

 savePerson() {
    this.personService.savePersonData(personId, this.personPayload).subscribe(
      () => {
      this.getevent.emit('');
      });
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular 4 - Calling a parent method from a child component not working

From Dev

Calling a method on Parent Component from Child Component

From Dev

react: Calling child component's method from parent component

From Dev

Calling a method in the parent component from a child component using blazor server

From Dev

Calling parent component method from child component written in Jsx Vue

From Dev

Angular 14 calling Child Component method from Parent Component

From Dev

Calling a method of a parent component from child - React Native

From Dev

Calling parent method from child?

From Dev

Calling parent functions from child component

From Dev

react child component fails to render when calling parent callback with setstate

From Dev

Vue `vm.$on()` callback not working in parent when event is emitted from dynamic component child

From Dev

calling a parent UIViewController method from a child UIViewController

From Dev

Calling a parent method from outside the child

From Dev

Python calling extended child method from parent

From Dev

Calling child method from obj of parent class

From Dev

Calling a method from child component in ionic 2

From Dev

How to get callback from child component to parent component in React Native

From Dev

Add callback function to parent component from child component

From Dev

Pass callback function from parent component to child component react

From Dev

Calling a children method of a functional component from parent

From Dev

VueJS - Communicating from child component to parent not working

From Dev

Call method from parent to child via component

From Dev

Trigger child component method from parent event

From Dev

Vuejs: Access a Parent Method from Child Component

From Dev

Calling a parent method from a child class with a variable of that child

From Dev

passing data from child to parent component - react - via callback function

From Dev

Passing a callback object from parent to child component angular

From

Calling child's method from parent's implementation method in golang

From Dev

React-Native - Calling Parent Method from Child Method

Related Related

  1. 1

    Angular 4 - Calling a parent method from a child component not working

  2. 2

    Calling a method on Parent Component from Child Component

  3. 3

    react: Calling child component's method from parent component

  4. 4

    Calling a method in the parent component from a child component using blazor server

  5. 5

    Calling parent component method from child component written in Jsx Vue

  6. 6

    Angular 14 calling Child Component method from Parent Component

  7. 7

    Calling a method of a parent component from child - React Native

  8. 8

    Calling parent method from child?

  9. 9

    Calling parent functions from child component

  10. 10

    react child component fails to render when calling parent callback with setstate

  11. 11

    Vue `vm.$on()` callback not working in parent when event is emitted from dynamic component child

  12. 12

    calling a parent UIViewController method from a child UIViewController

  13. 13

    Calling a parent method from outside the child

  14. 14

    Python calling extended child method from parent

  15. 15

    Calling child method from obj of parent class

  16. 16

    Calling a method from child component in ionic 2

  17. 17

    How to get callback from child component to parent component in React Native

  18. 18

    Add callback function to parent component from child component

  19. 19

    Pass callback function from parent component to child component react

  20. 20

    Calling a children method of a functional component from parent

  21. 21

    VueJS - Communicating from child component to parent not working

  22. 22

    Call method from parent to child via component

  23. 23

    Trigger child component method from parent event

  24. 24

    Vuejs: Access a Parent Method from Child Component

  25. 25

    Calling a parent method from a child class with a variable of that child

  26. 26

    passing data from child to parent component - react - via callback function

  27. 27

    Passing a callback object from parent to child component angular

  28. 28

    Calling child's method from parent's implementation method in golang

  29. 29

    React-Native - Calling Parent Method from Child Method

HotTag

Archive