Framework Integration
Embed in Angular
Show a Databrain dashboard or metric in an Angular app.
Before you start
- Your backend returns a guest token at
/api/databrain-token. Create a guest token - You have a dashboard id. Find the dashboard id
Steps
- Install the plugin.
npm install @databrainhq/plugin- Import the plugin once in
src/main.ts, beforebootstrapApplication.
import '@databrainhq/plugin/web';
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent, appConfig);- Add a standalone component with
CUSTOM_ELEMENTS_SCHEMA. Without the schema, Angular rejects thedbn-tags.
import {
Component,
CUSTOM_ELEMENTS_SCHEMA,
ElementRef,
Input,
OnInit,
signal,
ViewChild,
} from '@angular/core';
type DashboardElement = HTMLElement & {
handleServerEvent?: (...args: unknown[]) => unknown;
};
@Component({
selector: 'app-dashboard',
standalone: true,
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
@if (token(); as guestToken) {
@for (id of [dashboardId]; track id) {
<dbn-dashboard
#dashboard
[token]="guestToken"
[dashboardId]="id"
enable-download-csv="true"
[options]="options"
></dbn-dashboard>
}
}
`,
})
export class DashboardComponent implements OnInit {
@Input({ required: true }) dashboardId = '';
token = signal<string | undefined>(undefined);
options = { disableMetricCreation: true, hideDashboardName: true };
@ViewChild('dashboard')
set dashboard(ref: ElementRef<DashboardElement> | undefined) {
if (ref) ref.nativeElement.handleServerEvent = (event) => console.log(event);
}
async ngOnInit() {
const res = await fetch('/api/databrain-token');
this.token.set((await res.json()).token);
}
}- Use it in a template.
<app-dashboard dashboardId="your-dashboard-id"></app-dashboard>You see the dashboard in your app.
In an NgModule app, add schemas: [CUSTOM_ELEMENTS_SCHEMA] to the module that declares the component.
Show one metric
<dbn-metric [token]="token()" [metricId]="metricId"></dbn-metric>How the props work
[token],[dashboardId]and[options]set JS properties. Objects pass as they are.- A static attribute uses kebab case. Write booleans as
"true". - Functions go on the element through
@ViewChild. Assign an inline arrow function. Do not bind functions in the template. - The
@forblock withtrack idremounts the element when the dashboard changes.
If something goes wrong
- “‘dbn-dashboard’ is not a known element”. Add
CUSTOM_ELEMENTS_SCHEMAto the component’sschemas. - The embed shows the first dashboard of the data app, not yours. Bind
[dashboardId]to the id, not the dashboard name. customElements is not definedduring server-side rendering. Import the plugin insrc/main.tsonly, and wrap the element in a@deferblock.handleServerEventnever runs. Assign an inline arrow function. The element drops a named function.