但是:如果最后一个字母被删除,使输入为空,我想跳过 HTTP 请求等并返回一个空数组。
因此,我需要一个在管道中首先调用的运算符,每次满足条件时都会跳过下面的所有运算符并“提前返回”,就像 for 循环中的“break”或“return”语句一样。
我不能使用 filter(),因为该运算符会阻止生成的可观察值发出。但我需要它发出来清除下拉列表。
<input [formControl]="formGroup.searchTerm">
<ul>
<li *ngFor="let suggestion of suggestions$ | async">{{suggestion}}</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
suggetions$ = this.formGroup.valueChanges.pipe(
pluck('searchString')
// filter(searchString => searchString.length > 0) // does not help
unknownOperator(searchString => {
if(searchString.length === 0) {
skipOperatorsBelowAndReturnThisInstead([])
} else {
continueWithOperatorsBelow(searchTerm)
}
})
switchMap(values => this.http.get(this.url + values.term)),
map(this.buildSuggestions),
),
Run Code Online (Sandbox Code Playgroud)
谢谢!