简体   繁体   English

搜索throug服务器数据在Angular中不起作用

[英]Search throug server data does not work in Angular

Trying to search trough extrnal api Weather servis. 试图通过extrnal api天气服务搜索。

Api is tested and works fine. Api经过测试,工作正常。

Problem: Enter value in search bar and main Search method from servis is not called. 问题:在搜索栏中输入值,并且不会调用来自servis的主搜索方法。 That is reason why request is not sent. 这就是为什么不发送请求的原因。

I follow tutorial from angular official documentation: https://angular.io/tutorial/toh-pt6#search-by-name 我按照角度官方文档中的教程进行操作: https//angular.io/tutorial/toh-pt6#search-by-name

And this not work. 这不起作用。

Search Component: 搜索组件:

export class SearchCityComponent implements OnInit {
  cities$: Observable<City[]>
  private searchTerms = new Subject<string>()

  constructor(private weaterService: WeatherService) {}

  // Push a search term into the observable stream.
  search(term: string): void {
    this.searchTerms.next(term)
  }

  ngOnInit(): void {
    this.cities$ = this.searchTerms.pipe(
      // wait 300ms after each keystroke before considering the term
      debounceTime(300),

      // ignore new term if same as previous term
      distinctUntilChanged(),

      // switch to new search observable each time the term changes
      switchMap((term: string) => this.weaterService.searchCities(term))
    )
  }
}

Service: 服务:

 /* GET cities whose name contains search term */
  searchCities(term: string): Observable<City[]> {
    if (!term.trim()) {
      return of([])
    }
    return this.http
      .get<City[]>(`${this.citiesUrl}/?query=${term}`, httpOptions)
      .pipe(
        tap(_ => this.log(`found cities matching "${term}"`)),
        catchError(this.handleError<City[]>("searchCities", []))
      )
  }

When I enter in search input some value: 当我在搜索输入中输入一些值时:

In Search Comnponent in ngOnInit() last line of code is Not called search function from services? 在ngOnInit()中的搜索组件中,最后一行代码是不是来自服务的搜索功能?

 switchMap((term: string) => this.weaterService.searchCities(term))

You forgot subscribe with switchMap ! 你忘了订阅switchMap

As per the angular docs : 根据角度文档

An Observable instance begins publishing values only when someone subscribes to it. Observable实例仅在有人订阅时才开始发布值。 You subscribe by calling the subscribe() method of the instance, passing an observer object to receive the notifications. 您通过调用实例的subscribe()方法进行subscribe() ,传递一个observer对象来接收通知。

An observable will be triggered only if it's needed, to tell to an Observable that you need it you have to subscribe to it : 只有在需要时才会触发一个observable,告诉Observable你需要它,你必须订阅它:

    this.searchTerms.pipe(
      // wait 300ms after each keystroke before considering the term
      debounceTime(300),

      // ignore new term if same as previous term
      distinctUntilChanged(),

      // switch to new search observable each time the term changes
      switchMap((term: string) => this.weaterService.searchCities(term))
    ).subscribe()

Your implementation is alright. 你的实现没问题。 You just have to unwrap the cities$ Observable in your Template using an async pipe. 您只需使用async管道在模板中解包cities$ Observable。 It's recommended to use the async pipe wherever possible. 建议尽可能使用async管道。 Just makes the code less verbose. 只是让代码更简洁。

Doing something like this would help: 做这样的事情会有所帮助:

<ul>
  <li *ngFor="let city of cities$ | async">
    {{ city.name }}
  </li>
</ul>

Here's a Working Sample StackBlitz for your ref. 这是你的参考的工作样本StackBlitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM