angular2-observables

Returning value from subscription typescript

走远了吗. 提交于 2019-12-03 21:03:34
How would i return success from Save() method. public SaveItem() { if(save()){ // The goal is to use save method like this // Close pop up; } public SaveAndNew() { if(save()){ // The goal is to use save method like this // Create new item; } private save() { let issuccess = false; this.myservice.AddParty(newUserObject) .subscribe(data => { if (data['status'].toString() === '1') { return issuccess = false; } else { return issuccess = true; } }, (er) => { return issuccess = false; }); } If i have save(): boolean it will throw error that Must return a value if return issuccess outside the

angular2 guard not working on page refresh

为君一笑 提交于 2019-12-03 16:27:56
Before every request I want to be sure that there is a user profile available. I use a canActivateChild guard to do this. According to the documentation of angular2 it is possible to return an observable: https://angular.io/api/router/CanActivateChild app.routes.ts export const routes: Routes = [ { path: '', canActivateChild: [ProfileGuard], children: [ { path: 'home', component: HomeComponent, canActivate: [AuthGuard] }, { path: 'login', component: LoginComponent, canActivate: [GuestGuard] }, } ]; The canActivatedChild is executed before the child route canActivate profile.guard.ts: export

Angular 2 - test for change in route params

被刻印的时光 ゝ 提交于 2019-12-03 11:33:59
问题 I have a component in angular 2 which responds to changes in the route parameters (the component doesn't reload from scratch because we're not moving out of the main route. Here's the component code: export class MyComponent{ ngOnInit() { this._routeInfo.params.forEach((params: Params) => { if (params['area']){ this._pageToShow =params['area']; } }); } } This works a treat and _pageToShow is set appropriate on navigation. I'm trying to test the behaviour on a change to the route (so a second

Angular 2 - test for change in route params

一笑奈何 提交于 2019-12-03 01:57:41
I have a component in angular 2 which responds to changes in the route parameters (the component doesn't reload from scratch because we're not moving out of the main route. Here's the component code: export class MyComponent{ ngOnInit() { this._routeInfo.params.forEach((params: Params) => { if (params['area']){ this._pageToShow =params['area']; } }); } } This works a treat and _pageToShow is set appropriate on navigation. I'm trying to test the behaviour on a change to the route (so a second trigger of the observable but it's refusing to work for me.) Here's my attempt: it('sets PageToShow to

How do I stop Observable.Timer() or Observable.Interval() automatically after certain period of time

人走茶凉 提交于 2019-12-02 15:53:10
问题 public function(id: number) { this.periodicCheckTimer = Observable.timer(10000, 5000).subscribe( () => { let model = this.find(id); if (model['isActivated']) { this.periodicCheckTimer.unsubscribe(); } }); } I want to stop the timer automatically after 5 mins if the condition if(model['isActivated']) is not satisfied. However if the condition satisfies I can stop it manually. Not sure if the manual stop is still correct in this case. Any suggestions with other timer functions are also

timer.unsubscribe is not a function Angular2

≯℡__Kan透↙ 提交于 2019-12-02 00:42:36
问题 Tried to make a simple timer and need to break it on some if condition. But always got an error EXCEPTION: timer.unsubscribe is not a function . What am I doing wrong? let timer:any = Observable.timer(0,1000); timer.subscribe(data => { console.log(this.itemsRatedList); if(data == 5) timer.unsubscribe(); }); 回答1: It should be: let timer:any = Observable.timer(0,1000); let subscription = timer.subscribe(data => { console.log(this.itemsRatedList); if(data == 5) subscription.unsubscribe(); });

timer.unsubscribe is not a function Angular2

假装没事ソ 提交于 2019-12-01 21:54:34
Tried to make a simple timer and need to break it on some if condition. But always got an error EXCEPTION: timer.unsubscribe is not a function . What am I doing wrong? let timer:any = Observable.timer(0,1000); timer.subscribe(data => { console.log(this.itemsRatedList); if(data == 5) timer.unsubscribe(); }); It should be: let timer:any = Observable.timer(0,1000); let subscription = timer.subscribe(data => { console.log(this.itemsRatedList); if(data == 5) subscription.unsubscribe(); }); You can't unsubscribe an Observable , only a Subscription . Plunker example A more clear and simple example is

Angular 2: Observable / Subscription not triggering

吃可爱长大的小学妹 提交于 2019-11-30 17:22:24
I have done this multiple times in my App. It's simple, it should work... But this time it doesn't. My issue: I am calling a method in a service from a Component A, my Component B is subscribed but doesn't react nor receive anything. subscribe() is not triggering! navigation-elements.service.ts @Injectable() export class NavigationElementsService { updateIBOsNavigation$: Observable<any>; private updateIBOsNavigationSubject = new Subject<any>(); constructor() { this.updateIBOsNavigation$ = this.updateIBOsNavigationSubject.asObservable(); } updateIBOsNavigation(navigationData) { log.d(

Angular 2 Firebase Observable to promise doesn't return anything

纵饮孤独 提交于 2019-11-30 06:49:16
I'm currently working on an Angular 2 project with AngularFire2 and I'm trying to convert a FirebaseListObservable to a Promise. I know that it doesn't make much sense since Observables are more useful but this function will be part of another function where multiple promises are chained. And I'm not familiar with how to subscribe to Observables within a chain of promises... The function gets executed in the service, however it seems to not return anything. Basically, what I want to do is check in a Firebase list if an object with a certain name already exists and return either true or false.

RxJs Observables: run retryWhen after some more async requests

╄→гoц情女王★ 提交于 2019-11-30 03:35:36
问题 My use case is: User requests asset from our API which fails because of JWT expiring (passed as an httpOnly cookie) - API returns a 401 status code. We go and authenticate them (without the user doing anything) again using a refresh_token to retrieve a new JWT with a request from our client to auth0. We send that new JWT to our API to be set as an httpOnly cookie to replace the expired one. We then want to retry the original request the user made to the API in step 1. I'm trying to use