Search code examples
angularrxjsngrx

How can I pass value from one component from another component in Angular8 (Independent components)


I want to reset the value of a variable of another component. 2 components are independent ones. I want to reset the value of the particular variable. Once the value is updated need to refresh the UI of that component to reflect the changes.

Expecting something like Broadcast/Emit and On in AngularJS

Is it possible to do something with RxJS? If yes please share me some useful links.


Solution

  • you can use a Service to share an EventEmitter: https://angular.io/api/core/EventEmitter

    @Injectable()
    export class YourSharedService {
      private _event = new EventEmitter<any>();
    
      get event(): EventEmitter<any> {
        return this._event;
      }
    }
    

    Then just emit in the first component:

    this.yourService.event.emit();
    

    And subscribe in the other:

        this.yourService.event.subscribe(() =>  {});
    

    Usefull, only if your components are really independent.