我目前正在将Angular与Angular Instant Search结合使用。
1-根据URL参数更改动态更新HTML配置
读取网址中文件夹ID的更改
setUrlParams(){
this.activatedRoute.params.subscribe((urlParameters) => {
this.folder_id = urlParameters['folderId'];
this.setSearchConfig( this.folder_id);
});
}
更新我的Angular Instant Search配置
setSearchConfig(folder_id){
this.configOption = {
apiKey: "apiKey",
appId: "appID",
indexName: "projects",
routing: false,
searchParameters: {
facetsRefinements: {
project_folder: [folder_id]
}
},
};
}
HTML
<ais-instantsearch [config]="configOption">
<ais-search-box placeholder='Search for products'></ais-search-box>
<ais-hits>
<ng-template let-hits="hits">
<div *ngFor="let hit of hits">
Hit {{hit.objectID}}: {{hit.project_title}}
</div>
</ng-template>
</ais-hits>
</ais-instantsearch>
在初始加载时,这很好用。但是,在单击文件夹并更新ID时,正确的ID将传递到“ setSearchConfig”函数中。但是除非刷新页面,否则HTML不会更新。
问题 如何使HTML动态更新?
答案 0 :(得分:1)
已解决。
// 1. Create a search param object
searchParameters = {}
// 2. Call a function (setSearchParams) on url change
setUrlParams(){
this.activatedRoute.params.subscribe((urlParameters) => {
this.folder_id = urlParameters['folderId'];
this.setSearchConfig( this.folder_id);
});
}
// 3. Set the search param filters to the new folder id
setInstantSearchParams(folder_id){
this.searchParameters = {
filters: "project_folder: '" + folder_id + "'"
}
}
将以下内容添加到HTML
<ais-configure [searchParameters]="searchParameters"></ais-configure>