S
S
slowkazak2016-07-19 14:37:16
typescript
slowkazak, 2016-07-19 14:37:16

How to change the value of Observable in RxJs?

Good afternoon!

class Test
     {
         foo = Observable.of(1); // this.foo при определении будет равна 1
             constructor(){
                 this.foo.subscribe(val = > console.log(val) // при инициализации класса в консоль выведется 1
            }
     }

Actually the question is how to change the value of this.foo not from the subscriber and will the subscriber react to this change?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bromzh, 2016-07-19
@slowkazak

You need to read the docs. reactivex.io/documentation/observable.html
In general, there are a bunch of different ways, a bunch of nuances for subscriptions and subscribers, etc.
You can do this:

class Foo {
    observer: any;

    constructor() {
        let observable = Observable.create<number>(observer => this.observer = observer);

        let sub1 = observable.subscribe(
            value => console.log('Sub1. Value:', value),
            err => console.log('Sub1. Error:', err),
            () => console.log('Sub1. Complete')
        );

        this.observer.next(1);
        this.observer.next(2);
        this.observer.next(3);
        this.observer.complete();
    }
}

You can use Subject and its derivatives. There are examples in the documentation. Also with pictures!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question