Skip to content

NgDiagramService

The NgDiagramService provides advanced access to the diagram’s core API, including configuration, layout, event management, routing, transactions, and more.

private ngDiagramService = inject(NgDiagramService);
// Check if diagram is initialized
const ready = this.ngDiagramService.isInitialized();
// Update configuration
this.ngDiagramService.updateConfig({ gridSize: 20 });
  • NgDiagramBaseService

isInitialized: Signal<boolean>

Returns whether the diagram is initialized.

addEventListener<K>(event, callback): UnsubscribeFn

Add an event listener for a diagram event.

K extends keyof DiagramEventMap

K

The event name.

EventListener<DiagramEventMap[K]>

The callback to invoke when the event is emitted.

UnsubscribeFn

A function to unsubscribe.

const unsubscribe = ngDiagramService.addEventListener('selectionChanged', (event) => {
console.log('Selection changed', event.selectedNodes);
});

addEventListenerOnce<K>(event, callback): UnsubscribeFn

Add an event listener that will only fire once.

K extends keyof DiagramEventMap

K

The event name.

EventListener<DiagramEventMap[K]>

The callback to invoke when the event is emitted.

UnsubscribeFn

A function to unsubscribe.

ngDiagramService.addEventListenerOnce('diagramInit', (event) => {
console.log('Diagram initialized', event);
});

areEventsEnabled(): boolean

Check if event emissions are enabled.

boolean

True if events are enabled.


getActionState(): Readonly<ActionState>

Returns the current action state (readonly). This includes information about ongoing actions like resizing and linking.

Readonly<ActionState>

The current action state.


getConfig(): Readonly<NgDiagramConfig>

Returns the current configuration (readonly). The returned object cannot be modified directly — use updateConfig to make changes.

Readonly<NgDiagramConfig>

The current configuration.


getDefaultRouting(): string

Gets the current default routing name.

string

Name of the default routing.


getEnvironment(): EnvironmentInfo

Gets the current environment information.

EnvironmentInfo

The environment info object.


getRegisteredRoutings(): string[]

Gets all registered routing names.

string[]

Array of registered routing names.


hasEventListeners(event): boolean

Check if there are any listeners for an event.

keyof DiagramEventMap

The event name.

boolean

True if there are listeners.

if (ngDiagramService.hasEventListeners('selectionChanged')) {
// There are listeners for selection changes
}

registerMiddleware(middleware): () => void

Registers a new middleware in the chain.

Middleware

Middleware to register.

Function to unregister the middleware.

(): void

void


registerRouting(routing): void

Registers a custom routing implementation.

EdgeRouting

Routing implementation to register.

void

const customRouting: Routing = {
name: 'custom',
computePoints: (source, target) => [...],
computeSvgPath: (points) => '...'
};
ngDiagramService.registerRouting(customRouting);

removeAllEventListeners(): void

Remove all event listeners.

void

ngDiagramService.removeAllEventListeners();

removeEventListener<K>(event, callback?): void

Remove an event listener.

K extends keyof DiagramEventMap

K

The event name.

EventListener<DiagramEventMap[K]>

Optional specific callback to remove.

void

// Remove all listeners for an event
ngDiagramService.removeEventListener('selectionChanged');
// Remove a specific listener
ngDiagramService.removeEventListener('selectionChanged', myCallback);

setDefaultRouting(name): void

Sets the default routing to use when not specified on edges.

string

Name of the routing to set as default.

void


setEventsEnabled(enabled): void

Enable or disable event emissions.

boolean

Whether events should be emitted.

void

// Disable all events
ngDiagramService.setEventsEnabled(false);
// Re-enable events
ngDiagramService.setEventsEnabled(true);

startLinking(node, portId): void

Call this method to start linking from your custom logic.

Node

The node from which the linking starts.

string

The port ID from which the linking starts.

void


transaction(callback): void

Executes a function within a transaction context. All state updates within the callback are batched and applied atomically.

() => void

void

this.ngDiagramService.transaction(() => {
this.ngDiagramModelService.addNodes([node1, node2]);
this.ngDiagramModelService.addEdges([edge1]);
});

unregisterMiddleware(name): void

Unregister a middleware from the chain.

string

Name of the middleware to unregister.

void


unregisterRouting(name): void

Unregisters a routing implementation.

string

Name of the routing to unregister.

void


updateConfig(config): void

Updates the current configuration.

Partial<NgDiagramConfig>

Partial configuration object containing properties to update.

void