Search code examples
angularangular5angular6angular-uiangular2-components

How to call button of a component in other component and these two components are in different modules


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


Solution

  • 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
    })