Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
Call a method of the child component

I have a nested child component like this:

<app-main>
    <child-component />
</app-main>

My appMain component needs to invoke a method on child-component.

How to invoke a method on the child-component?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can get a reference to an element using

@ViewChild('childComponent') child;

where childComponent is a template variable <some-elem #childComponent>` or

@ViewChild(ComponentType) child;

where ComponentType is the type of a component or directive and then in ngAfterViewInit or an event handlers call child.someFunc().

ngAfterViewInit() {
  console.log(this.child);
}

See also get hold of an element in the template

over 4 years ago · Santiago Trujillo Relatório

0

Parent and child can communicate via data binding.

Example:

@Component({
    selector: 'child-component',
    inputs: ['bar'],
    template: `"{{ bar }}" in child, counter  {{ n }}`
})
class ChildComponent{
    constructor () {
        this.n = 0;
    }
    inc () {
        this.n++;
    }
}

@Component({
    selector: 'my-app',
    template: `
        <child-component #f [bar]="bar"></child-component><br>
        <button (click)="f.inc()">call child func</button>
        <button (click)="bar = 'different'">change parent var</button>
    `,
    directives: [ChildComponent]
})
class AppComponent {
    constructor () {
        this.bar = 'parent var';
    }
}

bootstrap(AppComponent);  

Demo

#f creates a reference to the child component and can be used in template or passed to function. Data from parent can be passed by [ ] binding.

over 4 years ago · Santiago Trujillo Relatório

0

Being a son component

@Component({
    // configuration
    template: `{{data}}`,
    // more configuration
})
export class Son {

  data: number = 3;

  constructor() { }

  updateData(data:number) {
    this.data = data;
  }

}

Having a father component

@Component({
    // configuration
})
export class Parent {
  @ViewChild(Son) mySon: Son;

  incrementSonBy5() {
    this.mySon.updateData(this.mySon.data + 5);
  }
}

In the father's template

<son></son>
<button (click)="incrementSonBy5()">Increment son by 5</button>

This solution only works for one <son></son>instance in the parent template. If you have more than one instance only will work in the first one of the template.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda