0

I want to call a button of a component in other component and these two components are in different module

  • Why do you need to call the button am not just share the functionality that gets called by the vutton? – Jonathan Stellwag Sep 17 '18 at 18:22
  • It is a big functionality , it wont be a good practise to write it again – user3082970 Sep 17 '18 at 18:25
  • 1
    Not to write it again but you can provide the functionality in a service and reuse the functionality everywhere you want. That's called dependency injection and seems to be the solution for the problem you have. https://angular.io/tutorial/toh-pt4 – Jonathan Stellwag Sep 17 '18 at 18:28
  • If you want to stick to your questions then the following describes a solution: https://stackoverflow.com/questions/37587732/how-to-call-another-components-function-in-angular2 – Jonathan Stellwag Sep 17 '18 at 18:31

1 Answers1

0

Use RxJs BehavourSubject !

You can declare BehavourSubject type variable in your service/any of the component and use that to communicate between the components.

As its event driven, once you change the value of the variable, it notify all who subscribed to it.

So when you click on the button in any component, other components know that, that button is clicked and you can do what ever you want in your component.

// this can be declared in a service to shared between components
let btnClk = new BehavourSubject('oo');

// in the first compnent
clickBtn() {
  btnClk.next('value to passed to other component');
}


// In the other component
btnClk.sunscribe((val)=>{
   // val is the value passed
})
Aniruddha Das
  • 20,520
  • 23
  • 96
  • 132
  • hi thanks it is working fine , one more question, i am calling a service in subscription of other component and i want response of it in first component – user3082970 Sep 18 '18 at 05:19