angular2-observables

How to make nested Observable calls in Angular2

ⅰ亾dé卋堺 提交于 2019-11-27 08:17:12
I am having some troubles making nested Observable calls. By that I mean a call to a http service that retrieve a user, then getting the id from the user to make another http call, and finally render the results on screen. 1) HTTP GET 1 : get the User 2) HTTP GET 2: get the User's preferences passing a unique identifier as a parameter This translates into the following code in component Blah.ts : version 1 - this code does not display anything ngOnInit() { this.userService.getUser() .flatMap(u => { this.user = u; // save the user return Observable.of(u); // pass on the Observable }) .flatMap(u

Testing error case with observables in services

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 07:28:10
Let's say I have a component that subscribes to a service function: export class Component { ... ngOnInit() { this.service.doStuff().subscribe( (data: IData) => { doThings(data); }, (error: Error) => console.error(error) ); }; }; The subscribe call takes two anonymous functions as parameters, I've managed to set up a working unit test for the data function but Karma won't accept coverage for the error one. I've tried spying on the console.error function, throwing an error and then expecting the spy to have been called but that doesn't quite do it. My unit test: spyOn(console,'error').and

Best way to import Observable from rxjs

允我心安 提交于 2019-11-27 06:10:08
In my angular 2 app I have a service that uses the Observable class from the rxjs library. import { Observable } from 'rxjs'; At the moment I am just using Observable so that I can use the toPromise() function. I read in another StackOverflow question somewhere that importing in this way and also importing from rxjs/Rx will import a whole lot of unnecessary stuff from the rxjs library that will increase the page load times and/or the code base. My question is, what is the best way to import Observable so I can use the toPromise() function without having to import everything else? Rxjs v 6.* It

Is it good way to call subscribe inside subscribe?

隐身守侯 提交于 2019-11-27 05:46:04
问题 this.service.service1().subscribe( res1 => { this.service.service1().subscribe( res2 => { this.service.service1().subscribe( res3 => { this.funcA(res1, res2, res3); }); }); }); I need to pass three data to one function from three different API's. Is it a good practice to subscribe inside a subscribe? If not, Please suggest the best way. 回答1: The correct way is to compose the various observables in some manner then subscribe to the overall flow - how you compose them will depend on your exact

Subject vs BehaviorSubject vs ReplaySubject in Angular

。_饼干妹妹 提交于 2019-11-27 02:31:37
I've been looking to understand those 3: Subject , Behavior subject and Replay subject . I would like to use them and know when and why, what are the benefits of using them and although I've read the documentation, watched tutorials and searched google I've failed to make any sense of this. So what are their purpose? A real-world case would be most appreciated it does not have to even code. I would prefer a clean explanation not just "a+b => c you are subscribed to ...." Thank you It really comes down to behavior and semantics. With a Subject - a subscriber will only get published values that

How to make synchronous http calls in angular 2

╄→尐↘猪︶ㄣ 提交于 2019-11-27 02:26:28
问题 This question has already been asked here. However, since the asker's application context is involved too much in the question, I couldn't understand the basics. For example, there is a queryArr parameter. What does it do? Anyway, I need a little bit of a guidance about how to make synchronous http calls in simplest way. The solution I came up with is that one has to subscribe to observables in a "nested" order. For example, there are observables ox and oy . Data of the request being called

How to throw observable error manually in angular2?

蹲街弑〆低调 提交于 2019-11-27 00:56:23
问题 I am working on angular2 app in which I am making a rest call through HTTp as below: login(email, password) { let headers = new Headers(); headers.append('Content-Type', 'application/x-www-form-urlencoded'); let options = new RequestOptions({ headers: headers }); let body = `identity=${email}&password=${password}`; return this.http.post(`${this._configService.getBaseUrl()}/login`, body, options) .map((res: any) => { let response: any = JSON.parse(res._body); if (response.success == 0) {

Angular2/4 : Refresh Data Realtime

空扰寡人 提交于 2019-11-27 00:23:48
问题 I need to refresh the data in a component page in an interval. Also I need to refresh the data after doing some action. I am using Obeservables in the service so that I can subscribe to when the response is ready. I am pushing the subscriptions to a object so that I can clear that on ngDestroy , I think, I have the following methods to achieve the same. Method 1 : setInterval I have set an interval on ngOnInit , which will call the refreshData in equal interval. The interval object will be

Testing error case with observables in services

佐手、 提交于 2019-11-26 17:39:24
问题 Let's say I have a component that subscribes to a service function: export class Component { ... ngOnInit() { this.service.doStuff().subscribe( (data: IData) => { doThings(data); }, (error: Error) => console.error(error) ); }; }; The subscribe call takes two anonymous functions as parameters, I've managed to set up a working unit test for the data function but Karma won't accept coverage for the error one. I've tried spying on the console.error function, throwing an error and then expecting

Merge subarrays using Observables

那年仲夏 提交于 2019-11-26 14:56:26
问题 I have this data structure: [{ id : 1, name : "Item 1", subItems : [{ id : 1, name : "SubItem 1" },{ id : 2, name : "SubItem 2" } ] }, { id : 2, name : "Item 2", subItems : [{ id : 3, name : "SubItem 3" }, { id : 4, name : "SubItem 4" } ] }] I make the following call to a web service to get the items: this.dataService.get("items") Returned is an Observable<Item[]> . What Observable operators can I use to only get a concatenated list of SubItems? I would like to end up with something like this