【发布时间】:2022-01-11 15:32:53
【问题描述】:
我有一个可观察的角度应用程序,它绑定到响应式表单下拉控件。我基本上需要过滤、排序并显示默认值。我写了两个实现,第一个执行过滤和排序但不是默认值,而后者只执行默认值。我能够将它们合并在一起并使其工作,但不确定这是否是最好的方法。有人可以对此有所了解吗
this.nameList$ = this.nameService.getNames()
.pipe(
map(response => response
.filter(t =>
t.name !== '!',
)
.sort(this.sortByNameAscending),
),
takeUntil(this.destroy$)
)
this.nameList$ = this.nameService.getNames()
.pipe(
tap((values: any) => {
const found = values.find(x => x.isDefault);
if (found) {
this.form.get('firstName').setValue(found.name);
}
})
,
takeUntil(this.destroy$)
)
我尝试过这个方法,但不确定它是否是最好的方法
this.nameList$ = this.nameService.getTitles()
.pipe(
map(response => response
.filter(t =>
t.name !== '!',
)
.sort(this.sortByNameAscending),
),
takeUntil(this.destroy$)
)
.pipe(
tap((response: any) => {
const found = response.find(x => x.isDefault);
if (found) {
this.form.get('firstName').setValue(found.name);
}
})
,
takeUntil(this.destroy$)
)
【问题讨论】:
-
当您更新问题时,我开始写评论。是的,你所拥有的是正确的,但你不需要另一个管道。将水龙头从第二根管子移到第一根管子上。所以它看起来像这样:
this.nameList$ = this.nameService.getTitles().pipe(map(...), tap(...), takeUntil(...))
标签: angular angular-reactive-forms