0

I am trying to share data between components across two different modules . I am using a shared service and BehaviorSubject to acheive this.

works perfectly in components in same module , but no luck for components in a different module. Any advice ?

Ram
  • 1
  • 1
  • 1
    What you have tried so far. https://stackoverflow.com/help/how-to-ask – Eldho Jan 03 '18 at 10:38
  • 1
    [This](https://angularfirebase.com/lessons/sharing-data-between-angular-components-four-methods/) article has a good description of this via dependency injection of a service. This should be best as it's implementing the Singleton and Observer patterns and it is very easy to scale and maintain. – Dimitar Nikovski Jan 03 '18 at 10:40
  • Refer this [**answer**](https://stackoverflow.com/questions/44867021/passing-value-from-one-component-to-other-component-using-input-property-when-us/44867072#44867072) – Aravind Jan 03 '18 at 10:41

3 Answers3

0

May be this one can solve your issue https://stackblitz.com/edit/angular-xrknuj

Ashraful Islam
  • 1,228
  • 10
  • 13
  • 1
    my case i have sharedmodule which is known to module 1 and module 2, shared service is known to sharedmodule (added in providers). component1 in module1 and component 2 in module 2 is subscribing a BehaviorSubject in shared service . data change triggered by component 1 is not subscribing in component 2 – Ram Jan 03 '18 at 12:16
  • Can you provide a stackblitz for that problem? – Ashraful Islam Jan 03 '18 at 17:06
0

Have you tried exporting your component from the shared module? If so, you can then use component outputs (if they are adjacent) or a service (if they are not adjacent) like you mentioned to share data between two components.

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { MyComponent } from '../components/my-component';

@NgModule({
  imports: [
    CommonModule,
  ],
  declarations: [
    MyComponent,
  ],
  exports: [
    MyComponent,
  ],
})

export class SharedModule { }
Stan
  • 476
  • 1
  • 5
  • 15
0

That should work with services across different modules as well. Do your modules know about each other?

A possible architecture could be like this: Module1 and Module2 want share some data. Then you have a Shared module, which imports BOTH, Module1 and Module2. In this case the communication should work.

Please see more information here: https://medium.com/dailyjs/3-ways-to-communicate-between-angular-components-a1e3f3304ecb (Your solution is no. 3)

dave0688
  • 5,372
  • 7
  • 33
  • 64
  • my case i have sharedmodule which is known to module 1 and module 2, shared service is known to sharedmodule (added in providers). component1 in module1 and component 2 in module 2 is subscribing a BehaviorSubject in shared service . data change triggered by component 1 is not subscribing in component 2 – Ram Jan 03 '18 at 12:13