angular2-observables

BehaviorSubject initial value not working with share()

﹥>﹥吖頭↗ 提交于 2019-12-10 13:22:51
问题 share() operator is applied to a BehaviorSubject. BehaviorSubject has initial value. Goal is to create a single shared subscribtion. But this shared subscribtion does not seem to work when BehaviorSubject has an initial value. Getting unexpected results. Code shown below: let subject = new Rx.BehaviorSubject(0); let published = subject .do(v => console.log("side effect")) .share(); published.subscribe((v) => console.log(v+" sub1")); published.subscribe((v) => console.log(v+" sub2")); subject

How to pass observable value to @Input() Angular 4

放肆的年华 提交于 2019-12-08 14:35:59
问题 I am new to angular and I have the following situation which is I have a service getAnswers():Observable<AnswerBase<any>[]> and two components that are related to each other. online-quote dynamic-form online-quote component calls the service getAnswers():Observable<AnswerBase<any>[]> in its ngOnInit() method and the result of this, is passed to the component dynamic-form . To illustrate the situation this is the code of my two components: online-quote.component.html: <div> <app-dynamic-form

angular 2 http exception handler and jwt refresh

一世执手 提交于 2019-12-08 05:53:41
问题 I am trying to refresh a JWT token when I got a certain exception, when it is another exception my ErrorHandler should handle them. I have one piece of code, one where the token refresh works, and one piece of code where the exception handler works, but I just can't combine them in a working way. The problem is that I can't throw an exception and catch it with my ErrorHandler in an observable. Here is the code where I can refresh my token with. When it fails it checks if the error code is

How to use retryWhen with a function that returns a Boolean?

≯℡__Kan透↙ 提交于 2019-12-07 20:55:52
问题 Here's my code: this._http.post(this._url_get + extension, '', { headers: headers }) .map(res => res['_body']) .retryWhen(errors => {return responseErrorProcess(errors)}) now I need to catch exceptions and pass them to my responseErrorProcess() which returns true if it needs to retry I could not figure out how to retrieve the exceptions from errors , this is how it looks: Subject_isScalar: falseclosed: falsehasError: falseisStopped: falseobservers: Array[0]thrownError: null__proto__:

Why handle errors with catchError and not in the subscribe error callback in Angular

和自甴很熟 提交于 2019-12-07 05:51:17
问题 So I'd normally write my http requests like so Service getData(){ return this.http.get('url') } Component getTheData() { this.service.getData().subscribe( (res) => { //Do something }, (err) => { console.log('getData has thrown and error of', err) }) But looking through Angular documentation they seem to format it like so in a Service getHeroes(): Observable<Hero[]> { return this.http.get<Hero[]>(this.heroesUrl) .pipe( catchError(this.handleError('getHeroes', [])) ); } What's the implicit

Waiting for an observable to finish

…衆ロ難τιáo~ 提交于 2019-12-07 02:48:46
问题 I have a method that needs to wait for an observable to finish. I know observable are great for returning single pieces of data over time but I need to know when this observable has completely finished returning all of its data so I can run validation code on the object it has returned. The method getCustom subscribes to an observable run on the supplied url which then returns the observable. I am not too sure if this is the best way to approach this situation so I would appreciate if anyone

RxJS Observable: Subscription lost?

微笑、不失礼 提交于 2019-12-06 22:19:05
问题 What is the difference between the following two observable mappings? (if something in the following code appears strange to you: it stems from a learning-by-doing hobby project; I still learn RxJS) I have a component with a getter and a constructor. Both read information from the app's ngrx store and extract a string ( name ). The only difference between the getter and the constructor: the getter is used in the HTML and the observable it returns is sent through an async pipe, whereas the

How to use retryWhen with a function that returns a Boolean?

三世轮回 提交于 2019-12-06 08:19:01
Here's my code: this._http.post(this._url_get + extension, '', { headers: headers }) .map(res => res['_body']) .retryWhen(errors => {return responseErrorProcess(errors)}) now I need to catch exceptions and pass them to my responseErrorProcess() which returns true if it needs to retry I could not figure out how to retrieve the exceptions from errors , this is how it looks: Subject_isScalar: falseclosed: falsehasError: falseisStopped: falseobservers: Array[0]thrownError: null__proto__: Observable` It doesn't seem to contain errors about the exceptions that occurs, plus I couldn't figure out what

Angular 2 Router Using BehaviorSubject Observable in Resolve

廉价感情. 提交于 2019-12-05 23:53:21
问题 I'm trying to set up my router config using a Resolve that returns an Observable from a BehaviorSubject. I've tried this in both angular 4.0.0-beta8 and angular 2.4.8+router 3.4.8 Here's my service: @Injectable() export class MyService { private _data: BehaviorSubject<Array<string>> = new BehaviorSubject(undefined); constructor() {} public getData(): Observable<Array<string>> { this._data.next(['test1', 'test2', 'test3']); let asObservable = this._data.asObservable().delay(1000); asObservable

Angular2 Observable Timer Condition

╄→尐↘猪︶ㄣ 提交于 2019-12-05 15:04:44
问题 I have a timer: initiateTimer() { if (this.timerSub) this.destroyTimer(); let timer = TimerObservable.create(0, 1000); this.timerSub = timer.subscribe(t => { this.secondTicks = t }); } How would I add the condition to after 60 minutes present a popup to the user? I've tried looking at a couple of questions (this and this) but it's not clicking for me. Still new to RxJS patterns... 回答1: You don't need RxJS for that. You can use good old setTimeout : initiateTimer() { if (this.timer) {