// #docregion import { Component, OnInit } from '@angular/core'; import { Observable, of } from 'rxjs'; import { catchError, startWith } from 'rxjs/operators'; import { TwainService } from './twain.service'; // #docregion component @Component({ selector: 'twain-quote', // #docregion template template: `

{{quote | async}}

{{ errorMessage }}

`, // #enddocregion template styles: [ `.twain { font-style: italic; } .error { color: red; }` ] }) export class TwainComponent implements OnInit { errorMessage: string; quote: Observable; constructor(private twainService: TwainService) {} ngOnInit(): void { this.getQuote(); } // #docregion get-quote getQuote() { this.errorMessage = ''; this.quote = this.twainService.getQuote().pipe( startWith('...'), catchError( (err: any) => { // Wait a turn because errorMessage already set once this turn setTimeout(() => this.errorMessage = err.message || err.toString()); return of('...'); // reset message to placeholder }) ); // #enddocregion get-quote } } // #enddocregion component