1
As Do, while in service, to call a function of the component that prompted this service?
1
As Do, while in service, to call a function of the component that prompted this service?
2
In Angular2, the Components should not have the logic of a business rule, they should stay in the Services , in the Components should be only things related to the application template, things related to data handling should be in the Service.
1
You can communicate via Subject
, in the concept of sending and receiving messages.
For example, your service will exist two functions, one that receives and one that sends.
@Injectable()
export class AlgumService{
private subject = new Subject<string>();
enviaMensagem(texto: string) {
this.subject.next(texto);
}
recebeMensagem(): Observable<string> {
return this.subject.asObservable();
}
}
In your other service, Voce calls the AlgumService
:
nomeMetodo() {
// envia mensagem
this.algumService.enviaMensagem('Seja bem vindo'):
}
and in your Component
in OnInit()
, Voce receives it:
this.algumService.recebeMensagem().subscribe(mensagem => {
console.log(mensagem); // Seja bem vindo
// chame sua funcao qualquer no componente
}, (error) => {
console.log(error);
})
Browser other questions tagged javascript angular
You are not signed in. Login or sign up in order to post.
I don’t think it’s possible to do that...
– flpn
I’m not an expert on Angular*, but in Angularjs you can implement a Watchers and publishing. It works like this: Your service has a method,
register()
, where you pass as current scope parameters and the function to be called (callback). On the service side you add the two to a collection of Watchers. At the desired moment you call allcallbacks
valid.– OnoSendai
Probably your application should not do this. has how you post the code as example?
– Eduardo Vargas