TheUnreal TheUnreal - 1 year ago 84
TypeScript Question

Subscribe after Unsubscribe()

I have an array of observables.

On my

method, I initialize my subscriptions.

this.lineOrdersSub[line.$key] = + 'line_orders/' + line.$key)
.subscribe((lineOrders) => {
this.line_orders[line.$key] = lineOrders;

I also have a checkbox for each subscription. When the checkbox is ticked (default), the subscription is active (subscribing to new data).

When I uncheck a checkbox, I do this:

unCheckmethod() {
this.lineOrdersSub[line.$key].unsubscribe(); //Unsubscribe to my subscription
this.line_orders[line.$key] = null; // Remove the data I get after the subscription (See first code snippet)

My uncheck method works well, but after I'm trying to start the subscription again with:

checkMethod() {

I get:

this.lineOrdersSub[line.$key].subscribe is not a function

My question:

  1. How I can subscribe to a subscription after I
    ? If impossible, Can you think of any other way to do this?

Answer Source

The error is correct. You're calling:

this.lineOrdersSub[line.$key] = ... .subscribe(...);

which means you're assigning a Subscription object to this.lineOrdersSub[line.$key]. Not an Observable.

Then calling .unsubscribe() obviously works as it should. When you try to call subscribe() it fails because Subscribtion object has no subscribe() method.

You have to subscribe to the Observable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download