Reputation: 3789
I have an observable that watches for a value change on a reactive form and then qets a new query based on the value change
// this need to run like this...on accountId.value changes if id is > 0 , otherwise accountMessagesDataSource$ = []
this.accountMessagesDataSource$ = this.piForm.get('accountId').valueChanges.pipe(
switchMap(query => this.pitching.getAccountMessage(query)),
tap(accountMessages => this.accountMessages = accountMessages)).subscribe();
I need so run this conditionally as described in the comment line above...
Upvotes: 1
Views: 2902
Reputation: 2487
you can use flatMap to compose your stream
this.accountMessagesDataSource$ = this.piForm.get('accountId').valueChanges.flatMap((id)=> id > 0 ? this.pitching.getAccountMessage(id):of([]))
Upvotes: 0
Reputation: 96891
So you want to run getAccountMessage
only when id > 0
and otherwise just return an empty array:
this.accountMessagesDataSource$ = this.piForm.get('accountId').valueChanges
.pipe(
switchMap(id => id > 0
? this.pitching.getAccountMessage(id)
: of([])
),
tap(accountMessages => this.accountMessages = accountMessages),
)
.subscribe();
Upvotes: 4