Application
So far, I placed the Se
You should use a Subject (either BehaviorSubject
or ReplaySubject
). The Subject
will act as both a producer and consumer. The consumer of it can subscribe to it, just like an observable. And the producer can use it to emit messages to consumers. For example
import { ReplaySubject } from 'rxjs/ReplaySubject'
@Injectable()
export class MapService {
private _currentMapView = new ReplaySubject<MayView>(1);
setCurrentView(mv: MapView){
this._currentView.next(mv);
}
get currentMapView$() {
return this._currentMapView.asObservable();
}
}
The subscriber just needs to suscribe
import { Subscription } from 'rxjs/Subscription';
export class SearchComponent {
sub: Subscription;
view: MapView;
constructor(private elRef:ElementRef, private mapService: MapService ) {
}
ngOnInit() {
this.sub = this.mapService.currentMapView$.subscribe(view => {
this.view = view;
})
}
ngOnDestroy() {
if (this.sub) {
this.sub.unsubscribe();
}
}
}
The MapViewComponent
just needs to call the setCurrentView
, and it will be handled by the subscribers automatically when it's sent
See Also:
Subject
/BehaviorSubject
/ReplaySubject