cre8
cre8

Reputation: 13562

Get value from subject in nginit

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

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

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

Related Questions