我有一个observable,它监视一个被动表单上的值更改,然后根据值更改来调查一个新查询
// 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();
我需要这样有条件地运行,如上面的评论专栏所述...
答案 0 :(得分:4)
所以你只想在getAccountMessage
时运行id > 0
,否则只返回一个空数组:
this.accountMessagesDataSource$ = this.piForm.get('accountId').valueChanges
.pipe(
switchMap(id => id > 0
? this.pitching.getAccountMessage(id)
: of([])
),
tap(accountMessages => this.accountMessages = accountMessages),
)
.subscribe();
答案 1 :(得分:0)
您可以使用flatMap撰写您的信息流
this.accountMessagesDataSource$ = this.piForm.get('accountId').valueChanges.flatMap((id)=> id > 0 ? this.pitching.getAccountMessage(id):of([]))