Skip to Content

Framework Integration

Embed in Angular

Show a Databrain dashboard or metric in an Angular app.

Before you start

Steps

  1. Install the plugin.
npm install @databrainhq/plugin
  1. Import the plugin once in src/main.ts, before bootstrapApplication.
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);
  1. Add a standalone component with CUSTOM_ELEMENTS_SCHEMA. Without the schema, Angular rejects the dbn- 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);
  }
}
  1. 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>

Find the metric id

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 @for block with track id remounts the element when the dashboard changes.

If something goes wrong

  • “‘dbn-dashboard’ is not a known element”. Add CUSTOM_ELEMENTS_SCHEMA to the component’s schemas.
  • The embed shows the first dashboard of the data app, not yours. Bind [dashboardId] to the id, not the dashboard name.
  • customElements is not defined during server-side rendering. Import the plugin in src/main.ts only, and wrap the element in a @defer block.
  • handleServerEvent never runs. Assign an inline arrow function. The element drops a named function.