Answer the question
In order to leave comments, you need to log in
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
}
}
Answer the question
In order to leave comments, you need to log in
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();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question