如何在valueChanges订阅中使用combineLatest运算符

问题描述 投票:0回答:1
showDateCriteria = combineLatest(this.myForm.controls['name'].valueChanges, this.myForm.controls['age'].valueChanges, (name, age) => ({name, age}))
.subscribe(val => !(val.name==null ||val.age==null ));

我用combineLatest运算符和showDateCriteria尝试了这段代码

<div *ngIf="{{showDateCriteria | async}}">Show This</div>

即使满足特殊条件,我也无法展示qazxsw poi

<div>

angular reactive-programming rxjs6
1个回答
1
投票

正如错误所说https://stackblitz.com/edit/angular-rs3d7a

你试图异步管道订阅,这是没有意义的。

你应该管道流。

ERROR Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'

编辑:

你也应该this.showDateCriteria = combineLatest( this.myForm.controls['name'].valueChanges, this.myForm.controls['age'].valueChanges, (name, age) => ({ name, age })); this.showDateCriteria.subscribe(val => !(val.name == null || val.age == null)); ,否则startWith将不会触发。此外,逻辑是错误的,当它们中的任何一个不为空时,你应该返回combineLatest boolean,如下所示:

true

this.showDateCriteria = combineLatest( this.myForm.controls['name'].valueChanges.pipe(startWith(null)), this.myForm.controls['age'].valueChanges.pipe(startWith(null)), (name, age) => { console.log(name); return name || age; });

© www.soinside.com 2019 - 2024. All rights reserved.