首页 > 解决方案 > 如何在角度 algolia 即时搜索中动态更改搜索参数

问题描述

我正在使用,并根据用户的输入<ais-configure [searchParameters]="{ aroundLatLng: '0, 0' }"></ais-configure>动态更新对象,但我看不到搜索结果的任何变化。{ aroundLatLng: '23, 90' }

这是一个例子,我在codesandbox中准备 - https://codesandbox.io/s/kxqxr0j45r

在这个例子中,你可以看到最初,我添加了以下搜索参数:

searchParameters = { hitsPerPage: 3 };

还有一个名为“更新搜索参数”的按钮,当您单击该按钮时,它会更改搜索参数,例如 -

updateParameter() { this.searchParameters.hitsPerPage = 10; }

我期望对 searchParameters 的任何更改都应反映在搜索结果中。据此,我应该每页看到 10 个结果。但它不起作用。

你能告诉我我怎样才能做到这一点吗?谢谢。

标签: angularalgoliainstantsearch

解决方案


我认为您需要触发搜索。您是否尝试过完成本教程,其中显示:

“将 InstantSearch.js 安装到应用程序的依赖项后,我们需要将 InstantSearch.js 库实例化为 Angular 服务。”

然后您可以利用 AppComponent 中的即时搜索服务:

...
import { InstantSearchService } from './services/instantsearch.service';
...
export class AppComponent {
...
    updateParameter() {
        ...
        this.instantSearchService.search.start(); // trigger the search
    }
}

推荐阅读