API Reference / Angular InstantSearch Widgets / ais-instantsearch
Aug. 23, 2019

ais-instantsearch

You are reading the documentation for Angular InstantSearch v3, which is in beta. You can find the v2 documentation here.

Widget signature
<ais-instantsearch
  [config]="{
    indexName: string
    searchClient: object
    // Optional parameters
    numberLocale: string
    searchFunction: function
    routing: boolean|object
  }"
></ais-instantsearch>

About this widget

The ais-instantsearch widget is a wrapper that lets you configure the credentials for search. This component automatically provides the search state to all its children.

Note that every other Angular InstantSearch widgets must be wrapped under this one.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import * as algoliasearch from 'algoliasearch/lite';

@Component({
  template: `
    <ais-instantsearch [config]="config">
      <!-- Widgets -->
    </ais-instantsearch>
  `,
})
export class AppComponent {
  config = {
    indexName: 'instant_search',
    searchClient: algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey'),
  }
}

Props

indexName
type: string
Required

The main index to search into.

1
2
3
4
5
6
7
8
9
10
11
12
13
@Component({
  template: `
    <ais-instantsearch [config]="config">
      <!-- Widgets -->
    </ais-instantsearch>
  `,
})
export class AppComponent {
  config = {
    // ...
    indexName: 'instant_search',
  }
}
searchClient
type: object
Required

Provides a search client to ais-instantsearch. To implement a custom search client, take a look at a the custom back-end guide.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import * as algoliasearch from 'algoliasearch/lite';

@Component({
  template: `
    <ais-instantsearch [config]="config">
      <!-- Widgets -->
    </ais-instantsearch>
  `,
})
export class AppComponent {
  config = {
    // ...
    searchClient: algoliasearch(
      'YourApplicationID',
      'YourSearchOnlyAPIKey'
    ),
  }
}
numberLocale
type: string
Optional

The locale used to display numbers. This is passed to .toLocaleString().

1
2
3
4
5
6
7
8
9
10
11
12
13
@Component({
  template: `
    <ais-instantsearch [config]="config">
      <!-- Widgets -->
    </ais-instantsearch>
  `,
})
export class AppComponent {
  config = {
    // ...
    numberLocale: 'fr',
  }
}
searchFunction
type: function
Optional

A hook that is called each time a search needs to be done, with the helper as a parameter. It’s your responsibility to call helper.search(). This option allows you, for example, to avoid doing searches at page load.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Component({
  template: `
    <ais-instantsearch [config]="config">
      <!-- Widgets -->
    </ais-instantsearch>
  `,
})
export class AppComponent {
  config = {
    // ...
    searchFunction(helper) {
      helper.search();
    }
  }
}
routing
type: boolean|object
default: false
Optional

The router configuration used to save the UI state into the URL, or any client-side persistence. You can find more information in the routing guide. The object form accepts two attributes:

  • router: object: this object saves the UI state. By default, it uses an instance of the history with the default parameters. It accepts:
    • onUpdate: function: adds an event listener that makes InstantSearch aware of external changes to the storage medium (e.g. the URL). Typically you’ll set up a listener for popstate, which triggers a callback with the current routeState.
    • read: function: reads the routing storage and gets a route object. It doesn’t take any parameters, and returns an object.
    • write: function: pushes a route object into a routing storage. Takes the UI state from the state mapping.
    • createURL: function: transforms a route object into a URL. It receives an object and returns a string (which may be empty).
    • dispose: function: cleans up all event listeners.

  • stateMapping: object: transforms the uiState into an object that can be saved by the router. The default value is provided by simple. It accepts:
    • stateToRoute: function: transforms a uiState representation into a route object. It receives an object that contains the UI state of all the widgets in the page. It can return any object that is readable by routeToState.
    • routeToState: function: transforms a route object into a uiState representation. It receives an object that contains the UI state stored by the router. It can return any object that is readable by stateToRoute.
1
2
3
4
5
6
7
8
9
10
11
12
13
@Component({
  template: `
    <ais-instantsearch [config]="config">
      <!-- Widgets -->
    </ais-instantsearch>
  `,
})
export class AppComponent {
  config = {
    // ...
    routing: true,
  }
}

HTML output

1
2
3
<div class="ais-InstantSearch">
  <!-- Widgets -->
</div>

Did you find this page helpful?