InstantSearch / Angular / V4 / API reference

Ais-Query-Rule-Context | Angular InstantSearch V4 (Deprecated)

Deprecated content
Angular InstantSearch is deprecated. Please use InstantSearch.js instead. For more information, see Migrating from Angular InstantSearch.

Signature

Signature
<ais-query-rule-context
  [trackedFilters]="object"
  // Optional parameters
  [transformRuleContexts]="function"
></ais-query-rule-context>

Import

1
2
3
4
5
6
7
8
import { NgAisQueryRuleContextModule } from 'angular-instantsearch';

@NgModule({
  imports: [
    NgAisQueryRuleContextModule,
  ],
})
export class AppModule {}

1. Follow additional steps in Optimize build size to ensure your code is correctly bundled.
2. This imports all the widgets, even the ones you don’t use. Read the Getting started guide for more information.

About this widget

The ais-query-rule-context widget lets you apply ruleContexts based on filters to trigger context dependent Rules.

Rules offer a custom experience based on contexts. You might want to customize the users’ experience based on the filters of the search (for example, they’re visiting the “Mobile” category, they selected the “Thriller” genre, etc.) This widget lets you map these filters to their associated Rule contexts, so you can trigger context based Rules on refinement.

Examples

1
2
3
<ais-query-rule-context
  [trackedFilters]="trackedFilters"
></ais-query-rule-context>
1
2
3
4
5
6
7
8
9
10
11
@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
})
export class AppComponent {
  // ...
  trackedFilters = {
    genre: () => ['Comedy', 'Thriller'],
    rating: values => values,
  }
}

Props

trackedFilters

Required
Type: object

The filters to track to trigger Rule contexts.

Each filter is a function which name is the attribute you want to track. They receive their current refinements as arguments. You can either compute the filters you want to track based on those, or return static values. When the tracked values are refined, it toggles the associated Rule contexts.

The added Rule contexts follow the format ais-{attribute}-{value} (for example ais-genre-Thriller). If the context of your Rule follows another format, you can specify it using the transformRuleContexts option.

Values are escaped to only consist of alphanumeric characters, hyphens, and underscores.

1
2
3
<ais-query-rule-context
  [trackedFilters]="trackedFilters"
></ais-query-rule-context>
1
2
3
4
5
6
7
8
9
10
11
@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
})
export class AppComponent {
  // ...
  trackedFilters = {
    genre: () => ['Comedy', 'Thriller'], // this tracks two static genre values
    rating: values => values, // this tracks all the rating values
  }
}

transformRuleContexts

Optional
Type: function

A function to apply to the Rule contexts before sending them to Algolia. This is useful to rename Rule contexts that follow a different naming convention.

1
2
3
4
<ais-query-rule-context
  // ...
  [transformRuleContexts]="transformRuleContexts"
></ais-query-rule-context>
1
2
3
4
5
6
7
8
9
@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
})
export class AppComponent {
  // ...
  transformRuleContexts = ruleContexts =>
    ruleContexts.map(ruleContext => ruleContext.replace('ais-', 'custom-'))
}
Did you find this page helpful?