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-highlight
  attribute="string"
  [hit]="object"
  // Optional parameters
  tagName="string"
></ais-highlight>
Import
1
2
3
4
5
6
7
8
import { NgAisHighlightModule } from 'angular-instantsearch';

@NgModule({
  imports: [
    NgAisHighlightModule,
  ],
})
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 renders any attribute from a hit into its highlighted form (when relevant). The hit property is an object as provided by ais-hits and ais-infinite-hits.

This component leverages the highlighting feature of Algolia and is designed to work with escapeHTML set to true in the surrounding <ais-hits></ais-hits>.

Examples

Basic usage

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

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-highlight attribute="meta.title" [hit]="hit"></ais-highlight>

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-highlight attribute="title" [hit]="hit"></ais-highlight>
    </div>
  </ng-template>
</ais-hits>

Properties

Parameter Description
attribute
type: string
Required

Attribute of the record to highlight. You can give a dot-separated value for deeply nested objects, like actor.name.

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

Original “hit” object, given from Hits or connectHits. Needs a _highlightResult[attribute].value to work.

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

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

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

HTML output

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