šŸŽ‰ Try the public beta of the new docs site at algolia.com/doc-beta! šŸŽ‰
UI libraries / Angular InstantSearch / Widgets

Angular InstantSearch isnā€™t compatible with Angularā€™s Ivy view engine. Weā€™re investigating how best to support this. For more information and to vote for Algoliaā€™s support of Angular 16 and beyond, see the GitHub issue Algolia Support for Angular InstantSearch

Signature
<ais-snippet
  attribute="string"
  [hit]="object"
  // Optional parameters
  tagName="string"
></ais-snippet>
Import
1
2
3
4
5
6
7
8
import { NgAisSnippetModule } from 'angular-instantsearch';

@NgModule({
  imports: [
    NgAisSnippetModule,
  ],
})
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 Highlight widget displays attributes in a shorter form (a snippet). Snippeted attributes are also highlighted.

It uses Algoliaā€™s snippeting feature with the hit object provided by ais-hits or ais-infinite-hits.

It requires escapeHTML to be set to true in the surrounding <ais-hits></ais-hits>.

To determine which attributes should be snippeted, first set them from the Algolia dashboard, the CLI, or with the API (using the attributesToSnippet parameter). With attributesToSnippet, you can also set the snippetā€™s size to a specific number of words (it defaults to 10).

Examples

Basic usage

1
<ais-snippet attribute="title" [hit]="hit"></ais-snippet>

Usage with nested properties

Given this record:

1
2
3
4
5
6
{
  "objectID": 1,
  "meta": {
    "title": "my title"
  }
}

You can access the highlighted version by specifying the path separating levels with dots:

1
<ais-snippet attribute="meta.title" [hit]="hit"></ais-snippet>

Usage within hits

1
2
3
4
5
6
7
8
<ais-hits>
  <ng-template let-hits="hits">
    <div *ngFor="let hit of hits">
      Hit {{hit.objectID}}:
      <ais-snippet attribute="title" [hit]="hit"></ais-snippet>
    </div>
  </ng-template>
</ais-hits>

Properties

Parameter Description
attribute
type: string
Required

Attribute of the record to snippet. For deeply nested objects, specify a dot-separated value like actor.bio.

1
<ais-snippet attribute="name"></ais-snippet>
hit
type: object
Required

Original ā€œhitā€ object, given from Hits or connectHits. Needs a _snippetResult[attribute].value to work.

1
<ais-snippet [hit]="hit"></ais-snippet>
tagName
type: string
default: "em"
Optional

Tag to use for the highlighted parts of the string. For example, mark.

1
<ais-snippet tagName="mark"></ais-snippet>

HTML output

1
<span class="ais-snippet">This is the <em class="ais-snippet-highlighted">highlighted text</em></span>
Did you find this page helpful?