Skip to content

Instance access

Use @ViewChild to get a reference to the Handsontable instance from your Angular component, then call API methods programmatically.

Use Handsontable’s API

You can programmatically change the internal state of Handsontable DataGrid beyond what’s possible with props. To do this, call the API methods of the relevant Handsontable instance associated with your instance of the HotTableComponent. Access to the Handsontable instance can be obtained as early as in the ngAfterViewInit() lifecycle hook by using the @ViewChild decorator, which holds a reference to the wrapper component.

The following example implements the HotTableComponent showing how to reference the Handsontable instance from the wrapper component.

TypeScript
/* file: app.component.ts */
import { Component, ViewChild } from '@angular/core';
import { GridSettings, HotTableComponent, HotTableModule} from '@handsontable/angular-wrapper';
@Component({
selector: 'example1-instance-access',
standalone: true,
imports: [HotTableModule],
template: ` <div class="example-controls-container">
<div class="controls">
<button (click)="selectCell()">Select cell B2</button>
</div>
</div>
<div>
<hot-table [data]="data" [settings]="gridSettings"></hot-table>
</div>`,
})
export class AppComponent {
@ViewChild(HotTableComponent, { static: true }) readonly hotTable!: HotTableComponent;
readonly data: string[][] = [
['SKU-4821', 'Stainless Steel Water Bottle', 'Harbor Goods', '142'],
['SKU-0093', 'Wireless Mouse', 'Alpine Supply Co.', '0'],
['SKU-1170', 'Ergonomic Office Chair', 'Cascade Distributors', '67'],
['SKU-2208', 'USB-C Charging Cable', 'Summit Trading', '215'],
];
readonly gridSettings: GridSettings = {
colHeaders: true,
height: 'auto',
autoWrapRow: true,
autoWrapCol: true
};
selectCell(): void {
// The Handsontable instance is stored under the `hotInstance` property of the wrapper component.
this.hotTable?.hotInstance?.selectCell(1, 1);
}
}
/* end-file */
/* file: app.config.ts */
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { registerAllModules } from 'handsontable/registry';
import { HOT_GLOBAL_CONFIG, HotGlobalConfig } from '@handsontable/angular-wrapper';
// register Handsontable's modules
registerAllModules();
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
{
provide: HOT_GLOBAL_CONFIG,
useValue: {
// license: 'your-license-key', // get a key at https://handsontable.com/pricing/
} as HotGlobalConfig,
},
],
};
/* end-file */
HTML
<div>
<example1-instance-access></example1-instance-access>
</div>

Call the API outside Angular’s zone

An API call can add DOM event listeners, for example when updateSettings() changes how the grid scrolls. A listener added inside Angular’s zone runs change detection each time its event fires. To keep those events out of change detection, call the API through NgZone.runOutsideAngular():

import { Component, NgZone, ViewChild, inject } from '@angular/core';
import { GridSettings, HotTableComponent, HotTableModule } from '@handsontable/angular-wrapper';
@Component({
standalone: true,
imports: [HotTableModule],
selector: 'app-example',
template: `
<button (click)="enablePagination()">Enable pagination</button>
<hot-table [settings]="gridSettings"></hot-table>
`,
})
export class AppComponent {
@ViewChild(HotTableComponent, { static: false }) readonly hotTable!: HotTableComponent;
private readonly ngZone = inject(NgZone);
readonly gridSettings: GridSettings = { height: 300 };
enablePagination() {
this.ngZone.runOutsideAngular(() => {
this.hotTable.hotInstance?.updateSettings({ pagination: true });
});
}
}

Settings passed through the [settings] input already run outside Angular’s zone.

Result

Your Angular component now holds a reference to the Handsontable instance through @ViewChild. You can call any Handsontable API method on that instance from within the component’s lifecycle hooks or event handlers.