How to call functions of the parent component in the child component?

Asked

Viewed 1,679 times

1

I have a child component staying at the father’s, as follows:

Parent component:

@Component({
  selector: 'app-componente-pai',
  template: `
<p id="elemento">componente pai</p>
<app-componente-filho></app-componente-filho>
`
})
export class Pai{

  quantVezesAlterado: number = 0;

  alterandoParagrafo(){
    let elem = document.getElementById('elemento');
    elem.innerHtml = 'componente pai alterado ' + this.quantVezesAlterado + ' vezes';
  }
}

Child component:

@Component({
  selector: 'app-componente-filho',
  template: `
<a>chamar função do pai</a>
<div>
    <!-- outros elementos -->
</div>
`
})
export class filho{ }

And I want to call the job alterandoParagrafo(), existing in the parent component, where the anchor <a> is clicked (and only the anchor, without affecting other components). How can I do this?

I’m using Angular 8.2.5

  • I don’t understand what the difficulty is, it’s not just putting an event to click the tag app-componente-filho?

1 answer

3


You can do this using the EventEmitter angular.

Add a Event Binding to the element of âncora:

son.componentts.

@Component({
  selector: 'app-componente-filho',
  template: `<a (click)="onClick()">chamar função do pai</a>`
})
export class filho { 
  @Output() callParent = new EventEmitter<any>();

  onClick(){
    this.callParent.emit(null);
  }
}

And add the Binding Event from callParent(the event being issued by the child component) in the parent component, passing as parameter the function you want to execute.

pai.component.ts

@Component({
    selector: 'app-componente-pai',
    template: `
  <p id="elemento">componente pai</p>
  <app-componente-filho (callParent)="onCallParent($event)"></app-componente-filho>
  `
  })
  export class Pai{

    quantVezesAlterado: number = 0;

    alterandoParagrafo(){
      let elem = document.getElementById('elemento');
      elem.innerHTML = 'componente pai alterado ' + this.quantVezesAlterado + ' vezes';
    }

    onCallParent(){
      this.alterandoParagrafo();
      this.quantVezesAlterado++;
    }
  }

To learn more, read the documentation of Angular

  • The question is misplaced. Your answer would make sense if he wanted to pass (issue) some value from child component to the parent component, But, all logic is already in the parent component, that is, the child element need not issue anything, just be clicked. Simply place a click event on the tag that renders the child component: <app-componente-filho (click)="alterandoParagrafo()"></app-componente-filho>

  • 1

    His answer would be to take into account that there is only one element within the child component which in this case would be the anchor. In this particular case it works, but any other type of situation is ineffective.

  • 1

    As I said, the question is not clear. My example was exactly based on the question, there is only one element to, so you don’t need to use Eventemitter(), why a method of issue and do not issue anything (null)?!

  • 1

    I see no problem in using emit(null), being an example present in the framework documentation.

  • 3

    Both solutions are functional, I believe it now falls to the creator of the question to decide which to use. No stress, man! :)

  • 1

    No problems Geovana, as you are new on the site, here the questions to be answered have to be very complete, with all possible details, so that the answer does not cause more confusion than clarifications both for those who asked the question, as for people who have the same doubt. I’m not stressed no :) Until pq hj is Friday!

  • 1

    @Leandrade the element may have only one <a>, but he could have text, images and others. Really, I thought I had implied this condition, but, rereading, I realize that I was not one hundred percent clear. I will update it.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.