ais-hits
<ais-hits // Optional parameters [transformItems]="function" ></ais-hits>
1
2
3
4
5
6
7
8
import { NgAisHitsModule } from 'angular-instantsearch';
@NgModule({
imports: [
NgAisHitsModule,
],
})
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
Use ais-hits
to display a list of results. To configure the number of hits to show, use one of:
- The
ais-hits-per-page
widget - The
ais-configure
widget and setting thehitsPerPage
insearchParameters
.
For guidance on how to search across more than one index, read the multi-index search guide.
Examples
1
<ais-hits></ais-hits>
Properties
transformItems
|
type: function
default: items => items
Optional
Receives the items and is called before displaying them. Should return a new array with the same shape as the original array. Useful for transforming, removing, or reordering items. In addition, the full If you’re transforming an attribute using the |
||
Copy
|
Templates
hits
|
type: object[]
The matched hits from the Algolia API. You can leverage the highlighting feature of Algolia with the |
||
results
|
type: object
The complete response from the Algolia API. It contains the |
||
Copy
|
|||
insights
|
type: function
signature: (method: string, payload: object) => void
Sends insights events.
|
||
Copy
|
HTML output
1
2
3
4
5
6
7
8
9
10
11
12
13
<div class="ais-Hits">
<ul class="ais-Hits-list">
<li class="ais-Hits-item">
...
</li>
<li class="ais-Hits-item">
...
</li>
<li class="ais-Hits-item">
...
</li>
</ul>
</div>
Customize the UI with connectHits
If you want to create your own UI of the ais-hits
widget, you can combine the connectHits
connector with the TypedBaseWidget
class.
1. Extend the TypedBaseWidget
class
First of all, you will need to write some boilerplate code to initialize correctly the TypedBaseWidget
class. This happens in the constructor()
of your class extending the TypedBaseWidget
class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { Component, Inject, forwardRef, Optional } from '@angular/core';
import { TypedBaseWidget, NgAisInstantSearch, NgAisIndex } from 'angular-instantsearch';
@Component({
selector: 'app-hits',
template: '<p>It works!</p>'
})
export class Hits extends TypedBaseWidget {
constructor(
@Inject(forwardRef(() => NgAisIndex))
@Optional()
public parentIndex: NgAisIndex,
@Inject(forwardRef(() => NgAisInstantSearch))
public instantSearchInstance: NgAisInstantSearch
) {
super('Hits');
}
}
There are a couple of things happening in this boilerplate:
- create a
Hits
class extendingTypedBaseWidget
- reference the
<ais-instantsearch>
parent component instance on theHits
widget class - set
app-hits
as a selector, so we can use our component as<app-hits></app-hits>
2. Connect your custom widget
The TypedBaseWidget
class has a method called createWidget()
which takes two arguments: the connector to use and an object of options
(instance options)
for this connector. We call this method at ngOnInit
. This component now implements OnInit
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { Component, Inject, forwardRef, Optional } from '@angular/core';
import { TypedBaseWidget, NgAisInstantSearch, NgAisIndex } from 'angular-instantsearch';
import connectHits, {
HitsWidgetDescription,
HitsConnectorParams
} from 'instantsearch.js/es/connectors/hits/connectHits';
@Component({
selector: 'app-hits',
template: '<p>It works!</p>'
})
export class Hits extends TypedBaseWidget<HitsWidgetDescription, HitsConnectorParams> {
public state: HitsWidgetDescription['renderState']; // Rendering options
constructor(
@Inject(forwardRef(() => NgAisIndex))
@Optional()
public parentIndex: NgAisIndex,
@Inject(forwardRef(() => NgAisInstantSearch))
public instantSearchInstance: NgAisInstantSearch
) {
super('Hits');
}
ngOnInit() {
this.createWidget(connectHits, {
// instance options
});
super.ngOnInit();
}
}
3. Render from the state
Your component instance has access to a this.state
property which holds the rendering options of the widget.
public state: HitsWidgetDescription['renderState'];
// {
// hits: object[];
// results: object;
// sendEvent: (eventType, hit, eventName) => void;
// }
1
2
3
4
5
<ul>
<li *ngFor="let hit of state.hits">
<ais-highlight attribute="name" [hit]="hit"> </ais-highlight>
</li>
</ul>
Rendering options
hits
|
type: object[]
The matched hits from the Algolia API. You can leverage the highlighting feature of Algolia through the |
results
|
type: object
The complete response from the Algolia API. It contains the |
sendEvent
|
type: (eventType, hit, eventName) => void
The function to send
|
Instance options
escapeHTML
|
type: boolean
default: true
Optional
Escapes HTML entities from hits string values. |
||
Copy
|
|||
transformItems
|
type: function
default: items => items
Optional
Receives the items and is called before displaying them. Should return a new array with the same shape as the original array. Useful for transforming, removing, or reordering items. In addition, the full If you’re transforming an attribute using the |
||
Copy
|
Full example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { Component, Inject, forwardRef, Optional } from '@angular/core';
import { TypedBaseWidget, NgAisInstantSearch, NgAisIndex } from 'angular-instantsearch';
import connectHits, {
HitsWidgetDescription,
HitsConnectorParams
} from 'instantsearch.js/es/connectors/hits/connectHits';
@Component({
selector: 'app-hits',
template: `
<ul>
<li *ngFor="let hit of state.hits">
<ais-highlight attribute="name" [hit]="hit"> </ais-highlight>
</li>
</ul>
`
})
export class Hits extends TypedBaseWidget<HitsWidgetDescription, HitsConnectorParams> {
public state: HitsWidgetDescription['renderState']; // Rendering options
constructor(
@Inject(forwardRef(() => NgAisIndex))
@Optional()
public parentIndex: NgAisIndex,
@Inject(forwardRef(() => NgAisInstantSearch))
public instantSearchInstance: NgAisInstantSearch
) {
super('Hits');
}
ngOnInit() {
this.createWidget(connectHits, {
// instance options
});
super.ngOnInit();
}
}
Sending Click and Conversion events
Please refer to the guide on Sending Insight Events to learn about sending events from any InstantSearch widget.