Skip to content

Instantly share code, notes, and snippets.

@pertrai1
Last active May 9, 2020 17:59
Show Gist options
  • Save pertrai1/b3e26f0f49b0a853fb6d8227d9a4291e to your computer and use it in GitHub Desktop.
Save pertrai1/b3e26f0f49b0a853fb6d8227d9a4291e to your computer and use it in GitHub Desktop.
RxJS Data Composition
<div *ngIf="product$ | async as products">
<ul>
<li *ngFor="let product of products">
{{ product.name }}
</li>
</ul>
</div>
@Component({
selector: 'my-data',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyDataComponent
implements OnInit {
products$ = this.service.product$
.pipe(
catchError(error => {
this.errorMessage = error;
return of(null)
})
);
}
@Injectable({
provideIn: 'root'
})
export class SomeService {
private someAPIUrl = '/api/'
product$ = this.http.get<Product[]>(this.someAPIUrl)
.pipe(
tap(console.log),
catchError(this.erroHandler)
);
constructor(
private http: HttpClient
) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment