Skip to content
npm

API service reference ​

NgxViewBuilderApiService is the one service your application talks to. Inject it anywhere, in a page component, a resolver, a service of your own, and use it to read and change the view on screen, react to what happens in it, and plug your own functions, validators, elements and icons in.

ts
import { inject } from '@angular/core';
import { NgxViewBuilderApiService } from '@ngxviewbuilder/runtime';

export class ClientPage {
  private readonly api = inject(NgxViewBuilderApiService);

  async save(): Promise<void> {
    const result = await this.api.validateData();
    if (result.isValid) {
      await this.clients.save(this.api.getData());
    }
  }
}

An application that hosts the builder imports the same service from @ngxviewbuilder/designer. It is the same class.

How a call reaches the view ​

You always inject the root service. What it talks to depends on what is on screen:

On screenCalls about the view (values, structure, elements, validation) go toRegistrations (functions, validators, elements, icons, theme) go to
<ngx-view-builder-runtime>that runtime viewthe root and every runtime view, now and later
several runtime viewsthe one created lastthe root and every runtime view
<nvb-scope>the scopethe root and every scope
<ngx-view-builder-designer>the view being edited (the builder owns it)the root, the builder and its Preview
nothing yetthe root itselfthe root; views created later pick them up

Two things follow from that table. You can register everything at startup, before any view exists, and every view gets it. And with one view on screen you never need a reference to it, because the root forwards. Several views on one page are different, see the next section.

The root API and a view's own API ​

There is one class, but there are several objects. inject(NgxViewBuilderApiService) gives you the root one, a single object for the whole app. Every <ngx-view-builder-runtime>, <ngx-view-builder-renderer> and <nvb-scope> makes one more for itself, so its data, structure and elements stay apart from every other view. A page with a nav bar view and a content view has three: the root, the nav bar's and the content's.

The component hands you its own one as api:

Root API, inject(NgxViewBuilderApiService)A view's own API, runtime.api
How manyone for the whole appone per view component
Existsalways, even before any viewwhile that view component exists
getData(), setValue(), getElement(), validateData()...go to the view created lastgo to this view, always
Events (onValueChanged, onComplete...)from every view on screenfrom this view
Registrations (functions, validators, elements, icons)reach every view, now and laterreach this view only
Use it forregistrations at startup, one view per page, listening to all viewsseveral views on one page

So with several views, the root is still right for registering things and for listening to everything, but a call like getData() on it reaches only the newest view. Ask the view itself instead:

ts
import { Component, viewChild } from '@angular/core';
import { NgxViewBuilderRuntime } from '@ngxviewbuilder/runtime';

@Component({
  imports: [NgxViewBuilderRuntime],
  template: `
    <ngx-view-builder-runtime #client [pageJson]="clientView" />
    <ngx-view-builder-runtime #order [pageJson]="orderView" />
  `,
})
export class ClientOrderPage {
  private readonly client = viewChild.required('client', { read: NgxViewBuilderRuntime });
  private readonly order = viewChild.required('order', { read: NgxViewBuilderRuntime });

  async save(): Promise<void> {
    const check = await this.client().api.validateData();
    if (check.isValid) {
      await this.clients.save(this.client().api.getData());
    }
    this.order().api.setValue('status', 'ready');
  }
}

With a single view on the page, viewChild.required(NgxViewBuilderRuntime) is enough. The template can reach the API too, through the export names nvbRuntime, nvbRenderer and nvbScope:

html
<ngx-view-builder-runtime #client="nvbRuntime" [pageJson]="clientView" />
<button type="button" (click)="client.api.validateData()">Check</button>

api exists as soon as the component does, but values and elements are there only after the view has rendered (see the tip below). On <ngx-view-builder> it is undefined until that component's view has initialised.

To tell views apart in events, listen on each view rather than on the root. Each component has outputs such as (valueChanged), and runtime.api.onValueChanged fires for that view only. A root onValueChanged payload has the dataPath but not the view it came from, so two views with a field of the same name look the same there.

Waiting for a view

Values and elements exist once the view has rendered. Subscribe to onAfterRender, or call the API from a button or an event handler, rather than straight from your constructor.

Reading this reference ​

Each page groups the methods of one area. Every method shows its signature, what it does, an example, and what it returns. Types are written in TypeScript; IStructure is the view JSON described in Structure JSON.

PageWhat it covers
Structure & settingsRead, replace and patch the view definition and its settings
Data & valuesForm data, single values, server errors, the dirty flag
ElementsFinding elements, reading and writing their properties, the element handle, rows of repeaters, element defaults
Validation & lifecycleValidating the view or any structure, toasts, render and completion notifications
Data sources & variablesReloading sources, host sources, websocket authorization, runtime variables and external context
Language & themeLanguage, locale, translations, UI dictionaries, theme modes, tokens and custom CSS
ExtensionsExpression functions, validator types, custom elements and properties, icons, table cells
Builder, templates & tablesTemplate library, element library, table settings and filters, builder tabs

Events have their own page: Events reference.

Every method at a glance ​

AreaMethods
StructuregetStructure · getStructureModel · setStructure · updateStructure · notifyStructureChanged · captureStructureSource · clearCapturedStructureSource
SettingsgetSettings · getSetting · setSetting · patchSettings · updateSettings · replaceSettings
DatagetData · setData · setElementsData · setValue · setElementValue · getElementValue · clearValue · clearValues · clearElementValue · resetElementValue · clearData · isDirty · markPristine · setElementErrors · clearElementErrors · getElementErrors
Element lookupgetElement · getElementModel · getElementLookup · getElementByDataPath · getElementBySchemaPath · getRenderedElements · getElementName and friends · describeElement
DOMgetElementDom · getElementDomById · getElementDomByTestId · queryRenderDom · queryRenderDomAll · getRenderRoot · registerRenderRoot
PropertiesgetElementProperty · setElementProperty · setElementProperties · removeElementProperty · removeElementProperties · resetElementProperty · resetElementProperties · updateElement · refreshElement
Element handleelement · getElementApi · rows · columns · cells
Element defaultssetElementTypeDefault · setElementTypeDefaults · setElementDefaults · defaults · getElementDefaults · clearElementDefaults · getElementTypeBuiltInDefaults
ValidationvalidateData · showToast · notifyValidationResult · notifyComplete · notifySaveRequested
Lifecyclerender notifications · notifyTabChanged · notifyCurrentPageChanged · notifyDialogClosed · element interaction notifications
Data sourcesreloadDataSource · reloadElementDataSource · reloadElementsByDataSource · setDefaultDataSources · setDataSourceTypeSettings · getDataSourceTypeSettings · setWebsocketAuthorizer
VariablessetRuntimeVariableContext · getRuntimeVariableContext · clearRuntimeVariableContext · setRuntimeVariableDefinitions · getRuntimeVariableDefinitions · configureRuntimeVariables
LanguagegetLanguage · setLanguage · resolveHostLanguage · applyHostLanguage · startLanguageSync · setApplicationLocale · getApplicationLocale · setLocale · setContentTranslations · setUiDictionaries · setUiTranslations · translateUi · setPropertyHints · setPropertyTypeHints
ThemesetThemeMode · getThemeMode · setTheme · setCustomTheme · getCustomTheme · setCssVariables · getCssVariables · setCustomCss · getCustomCss · setCustomCssUrls · getCustomCssUrls
ExtensionsregisterExtensions · registerExtensionsAsync · expression functions · validator types · getRegisteredValidatorTypes · clearRegisteredValidatorTypes · custom elements · registerElementGroup · element properties · SVG icons · table cell types · getTableHeaderCenterExtensions
TemplatesgetTemplates · loadTemplates · saveTemplate · deleteTemplate · setTemplateActionMap · registerTemplateActionDataSource
Element libraryloadSidebarGroups · saveSidebarGroup · deleteSidebarGroup
TablessetTableSettings · getTableSettings · setTableFilters · getTableFilters · setTableSavedFilters · getTableSavedFilters · request* methods
BuilderswitchToTab · requestTabChange · isBuilderAttached · getBuilderAdapter · attachBuilderAdapter · setAiAssistantConfig · getAiAssistantConfig