Skip to content

Instantly share code, notes, and snippets.

@ayyash
Created October 13, 2017 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayyash/8f3b664ffaac3c87898e9500ebe98319 to your computer and use it in GitHub Desktop.
Save ayyash/8f3b664ffaac3c87898e9500ebe98319 to your computer and use it in GitHub Desktop.
Angular2 conditional subscription to route param
<my-transaction-form [transaction]="pageTransaction$ | async"></my-transaction-form>
export class TransactionFormComponent implements OnInit {
pageTransaction$: BehaviorSubject<ITransaction> = new BehaviorSubject<ITransaction>(null);
ngOnInit(): void {
this._route.params
.switchMap(params => {
return this._transactionService.GetTransaction(params['id']);
})
.subscribe(data => {
// data successfully returned a transaction
this.pageTransaction$.next(data);
},
error => {
// if undefined parameter, create new instance
this.pageTransaction$.next(Transaction.NewTransaction(1));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment