在Angular应用中,我在我的组件中有这个Observable链:
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {
search.component.ts
results: any[] = [];
queryField: FormControl = new FormControl();
constructor(private _apiService: ApiService) { }
ngOnInit() {
this.queryField.valueChanges
.debounceTime(200)
.distinctUntilChanged()
.switchMap((query) => this._apiService.search(query)) // was subscribe
.subscribe(result => { this.results = result.items; console.log(result); });
} }
搜索.ccomponent.html
<input [formControl]='queryField' type="text" id="keyword" autocomplete="off" placeholder="start typing..."/>
我想做的是取消其余Observable链的执行,如果由formControl发出的值为null,则不继续转到switchMap操作符(以便不执行请求)。