две переменные поиска в машинописи API - PullRequest
0 голосов
/ 07 октября 2019

Я уверен, что я делаю что-то глупое здесь, но я не могу понять, как передать 2 переменные, используя подход, который я использую, который использует некоторые rxjs. У меня есть 2 переменные поиска для навигационного приложения, от и до, которые вводятся в поля searchFrom и searchTo в моем html, но я не могу понять, как это работает. либо searchTerms должен быть каким-то образом массивом или ... чем-то.

export class AppComponent implements OnInit {
  mapResult: any;
  to: any;
  from: any;

  private searchFrom: Subject<string>;
  private searchTo: Subject<string>;

  constructor(private mapQuestService: MapQuestService) {  }

  search(to: string): void {
    this.searchTo.next(to); }

  search(from: string): void {
      this.searchFrom.next(from); }



  ngOnInit() {

this.from = 'boston';
this.to = 'poughkeepsie';

this.searchTerms = new Subject<string>();
this.searchTerms.pipe(

    debounceTime(1000),

    distinctUntilChanged(),
    switchMap((to, from) => {
      return this.mapQuestService.getMap(this.to, this.from);

  ))
  .subscribe((result: any) => {

    this.mapResult = result.items;
  });
}

}

есть идеи?

1 Ответ

3 голосов
/ 07 октября 2019

Попробуйте использовать ОбъединитьПоследний вместо другой темы:

this.searchTerms = combineLatest(this.to, this.from);
this.searchTerms.pipe(
  debounceTime(1000),
  distinctUntilChanged(),
  switchMap((to, from) => this.mapQuestService.getMap(to, from))
)
.subscribe((result: any) => this.mapResult = result.items);

Демонстрация Stackblitz две наблюдаемые

...