angular2-observables

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

断了今生、忘了曾经 提交于 2019-12-05 11:11:45
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 upside of this as it seems quite verbose to me and I've personally never had the need to pipe my errors. 1

The pipe 'async' could not be found

北慕城南 提交于 2019-12-05 08:46:33
问题 I am trying to build a simple blog with Angular 2 and Firebase and I am having issues using async pipe in component. I get the error in the console. zone.js:344Unhandled Promise rejection: Template parse errors: The pipe 'async' could not be found (" [ERROR ->]{{ (blog.user | async)?.first_name }} "): BlogComponent@6:3 ; Zone: ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors: The pipe 'async' could not be found (" blog.component.ts import {Component,

Waiting for an observable to finish

一笑奈何 提交于 2019-12-05 05:52:56
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 could give me any advice or a direction to handle this. private validateQuoteRetrievalAnswers(reference

angular2 guard not working on page refresh

时间秒杀一切 提交于 2019-12-05 02:00:10
问题 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] }

Pass Observable data from Parent Component to a Child Component created with componentFactoryResolver in Angular 2+

流过昼夜 提交于 2019-12-04 09:44:18
I need help as I am a bit lost right now. So, I have a component that dynamically injets a child component into its template using componentFactoryResolver , here is my HTML <div class="dialog"> <div #container></div> <button (click)="move('back')">Back</button> <button (click)="move('forwards')">Forwards</button> </div> also in my component I have an observable that captures the click of the buttons like so, here is my (edited / reduced) code // parent-component.ts @ViewChild('container', {read: ViewContainerRef}) public dialogContainer: ViewContainerRef; public navigationClick$: Observable

Angular 2 Router Using BehaviorSubject Observable in Resolve

左心房为你撑大大i 提交于 2019-12-04 04:56:43
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.subscribe((myData) => { console.log([myData, 'this console message DOES show up']); }); // if I return

'Error' message: 'Property 'from' does not exist on type 'typeof Observable'

人走茶凉 提交于 2019-12-04 03:44:59
I am trying to learn reactive programming using RxJS. I was trying to create an observable from an array using Observable.from() method, but I am getting an error: Property 'from' does not exist on type 'typeof Observable' I scaffolded an Angular application using Angular CLI, so all the dependencies including RxJS package was imported correctly. In app.component.ts I added below import statements: import { Observable} from 'rxjs/Observable' import 'rxjs/observable/from' And my AppComponent class looks like this: export class AppComponent { numbers: number[] = [1, 2, 3, 4, 5]; callMyObservable

Angular - what is the preferred way to terminate Observables?

时光毁灭记忆、已成空白 提交于 2019-12-04 03:16:29
From my understanding of Angular and RxJs there are two ways to terminate Observables. You can unsubscribe() from them or use takeUntil() and complete() . Below are examples of each approach (in pseudocode). The unsubscribe() approach private _id: number; private _subscriptions: Subscription[] = []; constructor(private _route: ActivatedRoute) { this._getId(); } public ngOnDestroy(): void { this._subscriptions.forEach( subscription => subscription.unsubscribe() ); } private _getId(): void { this._subscriptions.push( this._route.params.subscribe(params => this._id = +params['id']) ); } The

angular2 ngFor not working while fetching data from api on ngOnInit()

拥有回忆 提交于 2019-12-04 01:28:46
问题 comment.component.ts: import { Component, OnInit } from '@angular/core'; import { Router} from '@angular/router' import { Comment } from 'comment entity path' import {CommentService} from 'comment service path' import { Observable } from 'rxjs/Observable'; @Component({ template: ` <ul><li *ngFor="let comment of comments|async"> {{comment.Name}}</li></ul>` }) export class CommentComponent implements OnInit { comments: Observable<comment[]>; constructor(private router: Router, private

The pipe 'async' could not be found

随声附和 提交于 2019-12-03 23:33:11
I am trying to build a simple blog with Angular 2 and Firebase and I am having issues using async pipe in component. I get the error in the console. zone.js:344Unhandled Promise rejection: Template parse errors: The pipe 'async' could not be found (" [ERROR ->]{{ (blog.user | async)?.first_name }} "): BlogComponent@6:3 ; Zone: ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors: The pipe 'async' could not be found (" blog.component.ts import {Component, Input} from "@angular/core"; @Component({ selector: 'blog-component', templateUrl: './blog.component