Created
October 13, 2017 11:31
-
-
Save ayyash/8f3b664ffaac3c87898e9500ebe98319 to your computer and use it in GitHub Desktop.
Angular2 conditional subscription to route param
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
<my-transaction-form [transaction]="pageTransaction$ | async"></my-transaction-form> |
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
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