Reputation: 13562
I need some values from a service with an http call so I save them in a subject.
This works fine in my component with
ngOnInit() {
this.service.values.subscribe(values) {
console.log(values);
}
}
But when I the ngOnInit
will be fired again I don't get any values because the service is only triggered when the whole website is reloaded (not when I switch to another page).
So how is it possible to subscribe to elements when they change but also get them when they are already inside the subject?
Upvotes: 1
Views: 982
Reputation: 658037
I guess what you want is to use a BehaviorSubject
or ReplaySubject
(without initial value) which returns the last emitted value to new subscribers.
Upvotes: 1