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

166
Visualizações
Call function from one component from a component Angular

I'm a beginner in Angular. I've been facing this issue with calling a method in a component from a component, which they are not related to. I followed a lot of tutorials on the internet but haven't found the solution.

So the detail is: There are 2 unrelated components. The first component has a button and if I click that button, the string from the first component should be sent to a function in the second component and in that function It should display the string into the console. But the problem I'm facing here is that the function is called only once before I click and it just displays the value "111" which is the default value. Please help me

First component:

export class FirstComponent implements OnInit{
  constructor(location:Location, private renderer : Renderer2, private element : ElementRef, private router: Router, private httpClient: HttpClient, private servic: MainService) {
  }
  clickMe() {
     this.servic.sendMessage("001");
  }
}

Second component:

export class SecondComponent implements OnInit {
  clickEventSubs:Subscription;
  constructor(public servic: MainService, private spinner: NgxSpinnerService, private router: Router){} 
    this.clickEventSubs = this.servic.receiveMessage().subscribe(message => {
      this.toggle(message);
    })
  public toggle(state: string){
    console.log(state);
  }
}

Shared service:

@Injectable({
  providedIn: 'root'
})
export class MainService {
  private message = new BehaviorSubject<string>("111");
  sendMessage(mess:string) {
    this.message.next(mess);
  }
  receiveMessage(): Observable<any> {
    return this.message.asObservable();
  }
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When relationships between component is Child > Parent - Share date via @viewChild()

In First Component, Use @ViewChild() and pass second component name as an argument -

@ViewChild(SecondComponent) secondChildView!: SecondComponent;

Then call the second component's function in the first component's function -

 import {ViewChild} from '@angular/core';

 export class FirstComponent implements OnInit{
 @ViewChild(SecondComponent) secondChildView!: SecondComponent;

 constructor() {}
 
   clickMe() {
     this.secondChildView.toggle('001'); 
  }
}

Whenever you call clickMe function it will call the toggle function of second component and you will get the value in toggle function from the first component.

export class SecondComponent implements OnInit {

 constructor(){} 

 public toggle(state: string){
  console.log(state);
 }
}
about 4 years ago · Juan Pablo Isaza 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