如果没有给出搜索参数,则返回所有结果

发布于 2025-01-10 05:14:20 字数 1453 浏览 5 评论 0原文

我正在查看 Deborah Kurata 的英雄之旅的声明式 RxJS 方法。我想知道在以下情况下它将如何工作:

  1. 默认情况下,加载所有英雄
  2. 如果有搜索词,则显示搜索结果

现在,通过执行以下操作可能可以实现:

hero-search.component.ts :

  export class HeroSearchComponent {
  // DJK3 Assign to the declared Observable in the service
  heroes$ = this.heroService.filteredHeroes$;
  allHeroes$ = this.heroService.heroes$;

  searchActive = false;

  constructor(private heroService: HeroService) {}

  // Push a search term into the observable stream.
  search(term: string): void {
    this.heroService.search(term);
    this.searchActive = true;
  }
}

hero-search.component.html:

<div id="search-component">
  <label for="search-box">Hero Search</label>
  <input #searchBox id="search-box" (input)="search(searchBox.value)" />

  <ng-container *ngIf="!searchActive; else searchResults">
    {{allHeroes$ |async | json}}
  </ng-container>


  <ng-template #searchResults>
    <ul class="search-result">
      <li *ngFor="let hero of heroes$ | async">
        <a routerLink="/detail/{{hero.id}}">
          {{hero.name}}
        </a>
      </li>
    </ul>
  </ng-template>

</div>

但这对我来说看起来是错误的。有更好的方法吗?也许直接在服务中检查这个?

I was looking at the declarative RxJS approach to tour of heroes by Deborah Kurata. I would like to know how it would work in the following case:

  1. By default, load all heroes
  2. If there's a search term, show the search results

Now, this would be probably possible by doing something like this:

hero-search.component.ts:

  export class HeroSearchComponent {
  // DJK3 Assign to the declared Observable in the service
  heroes$ = this.heroService.filteredHeroes$;
  allHeroes$ = this.heroService.heroes$;

  searchActive = false;

  constructor(private heroService: HeroService) {}

  // Push a search term into the observable stream.
  search(term: string): void {
    this.heroService.search(term);
    this.searchActive = true;
  }
}

hero-search.component.html:

<div id="search-component">
  <label for="search-box">Hero Search</label>
  <input #searchBox id="search-box" (input)="search(searchBox.value)" />

  <ng-container *ngIf="!searchActive; else searchResults">
    {{allHeroes$ |async | json}}
  </ng-container>


  <ng-template #searchResults>
    <ul class="search-result">
      <li *ngFor="let hero of heroes$ | async">
        <a routerLink="/detail/{{hero.id}}">
          {{hero.name}}
        </a>
      </li>
    </ul>
  </ng-template>

</div>

But this looks wrong to me. Is there a better approach? Maybe checking this directly in the service?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩虹直至黑白 2025-01-17 05:14:20

有多少英雄?

如果不是太多,那么我会想象 allHeroes$ 由一个服务创建,该服务在内部类似于 this.httpClient.get(methodName, options).pipe(shareReplay (1)) 然后在服务上有一个方法接受 Observable 并返回 combineLatest([this.allHeroes$, arg]).pipe(map(([heroes, searchTerm]) => !searchTerm ? of(heroes) :heroes.filter(insertFormulaHere))) (...至少在最初是这样,但这可能是 然后,该组件将实例化

一个可观察对象,该可观察对象监听搜索框的更改,并将其传递给服务方法,将响应保存到组件的heroes$ 属性。

How many heroes?

If not too many, then I'd imagine allHeroes$ to be created by a service that, internally, would be something like this.httpClient.get(methodName, options).pipe(shareReplay(1)) and then have a method on the service that accepted an Observable<string> and returned combineLatest([this.allHeroes$, arg]).pipe(map(([heroes, searchTerm]) => !searchTerm ? of(heroes) : heroes.filter(insertFormulaHere))) (... at least initially, but this could be optimized after a working prototype was achieved.)

Then, the component would instantiate an observable that listened to changes to the search box, and pass it to the service method, saving the response to the component's heroes$ property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文