user8836786
user8836786

Reputation:

Angular Instant Search - Update HTML config based on URL params

I'm currently using Angular with Angular Instant Search.

1 - Dynamically update the HTML config based on URL param change

Reading the change of folder id in the url

    setUrlParams(){
       this.activatedRoute.params.subscribe((urlParameters) => {    
          this.folder_id = urlParameters['folderId'];
          this.setSearchConfig( this.folder_id);    
        });
    }

Updating my Angular Instant Search config

  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>

On the initial load, this works great. However, when clicking on a folder and updating the ID, the correct ID is passed into the "setSearchConfig" function. But the HTML isn't updating unless I refresh the page.

Question How do I make the HTML dynamically update?

Upvotes: 0

Views: 273

Answers (1)

user8836786
user8836786

Reputation:

Solved.

// 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 + "'"
   }
}

Add the following to the HTML

<ais-configure [searchParameters]="searchParameters"></ais-configure>

Upvotes: 1

Related Questions