简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM