Last active
May 9, 2020 17:59
-
-
Save pertrai1/b3e26f0f49b0a853fb6d8227d9a4291e to your computer and use it in GitHub Desktop.
RxJS Data Composition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div *ngIf="product$ | async as products"> | |
<ul> | |
<li *ngFor="let product of products"> | |
{{ product.name }} | |
</li> | |
</ul> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) | |
}) | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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