NgDiagramService
The NgDiagramService
provides advanced access to the diagram’s core API,
including configuration, layout, event management, routing, transactions, and more.
Example usage
Section titled “Example usage”private ngDiagramService = inject(NgDiagramService);
// Check if diagram is initializedconst ready = this.ngDiagramService.isInitialized();
// Update configurationthis.ngDiagramService.updateConfig({ gridSize: 20 });
Extends
Section titled “Extends”NgDiagramBaseService
Properties
Section titled “Properties”isInitialized
Section titled “isInitialized”isInitialized:
Signal
<boolean
>
Returns whether the diagram is initialized.
Methods
Section titled “Methods”addEventListener()
Section titled “addEventListener()”addEventListener<
K
>(event
,callback
):UnsubscribeFn
Add an event listener for a diagram event.
Type Parameters
Section titled “Type Parameters”K
extends keyof DiagramEventMap
Parameters
Section titled “Parameters”K
The event name.
callback
Section titled “callback”EventListener
<DiagramEventMap
[K
]>
The callback to invoke when the event is emitted.
Returns
Section titled “Returns”UnsubscribeFn
A function to unsubscribe.
Example
Section titled “Example”const unsubscribe = ngDiagramService.addEventListener('selectionChanged', (event) => { console.log('Selection changed', event.selectedNodes);});
addEventListenerOnce()
Section titled “addEventListenerOnce()”addEventListenerOnce<
K
>(event
,callback
):UnsubscribeFn
Add an event listener that will only fire once.
Type Parameters
Section titled “Type Parameters”K
extends keyof DiagramEventMap
Parameters
Section titled “Parameters”K
The event name.
callback
Section titled “callback”EventListener
<DiagramEventMap
[K
]>
The callback to invoke when the event is emitted.
Returns
Section titled “Returns”UnsubscribeFn
A function to unsubscribe.
Example
Section titled “Example”ngDiagramService.addEventListenerOnce('diagramInit', (event) => { console.log('Diagram initialized', event);});
areEventsEnabled()
Section titled “areEventsEnabled()”areEventsEnabled():
boolean
Check if event emissions are enabled.
Returns
Section titled “Returns”boolean
True if events are enabled.
getActionState()
Section titled “getActionState()”getActionState():
Readonly
<ActionState
>
Returns the current action state (readonly). This includes information about ongoing actions like resizing and linking.
Returns
Section titled “Returns”Readonly
<ActionState
>
The current action state.
getConfig()
Section titled “getConfig()”getConfig():
Readonly
<NgDiagramConfig
>
Returns the current configuration (readonly). The returned object cannot be modified directly — use updateConfig to make changes.
Returns
Section titled “Returns”Readonly
<NgDiagramConfig
>
The current configuration.
getDefaultRouting()
Section titled “getDefaultRouting()”getDefaultRouting():
string
Gets the current default routing name.
Returns
Section titled “Returns”string
Name of the default routing.
getEnvironment()
Section titled “getEnvironment()”getEnvironment():
EnvironmentInfo
Gets the current environment information.
Returns
Section titled “Returns”The environment info object.
getRegisteredRoutings()
Section titled “getRegisteredRoutings()”getRegisteredRoutings():
string
[]
Gets all registered routing names.
Returns
Section titled “Returns”string
[]
Array of registered routing names.
hasEventListeners()
Section titled “hasEventListeners()”hasEventListeners(
event
):boolean
Check if there are any listeners for an event.
Parameters
Section titled “Parameters”keyof DiagramEventMap
The event name.
Returns
Section titled “Returns”boolean
True if there are listeners.
Example
Section titled “Example”if (ngDiagramService.hasEventListeners('selectionChanged')) { // There are listeners for selection changes}
registerMiddleware()
Section titled “registerMiddleware()”registerMiddleware(
middleware
): () =>void
Registers a new middleware in the chain.
Parameters
Section titled “Parameters”middleware
Section titled “middleware”Middleware to register.
Returns
Section titled “Returns”Function to unregister the middleware.
():
void
Returns
Section titled “Returns”void
registerRouting()
Section titled “registerRouting()”registerRouting(
routing
):void
Registers a custom routing implementation.
Parameters
Section titled “Parameters”routing
Section titled “routing”EdgeRouting
Routing implementation to register.
Returns
Section titled “Returns”void
Example
Section titled “Example”const customRouting: Routing = { name: 'custom', computePoints: (source, target) => [...], computeSvgPath: (points) => '...'};ngDiagramService.registerRouting(customRouting);
removeAllEventListeners()
Section titled “removeAllEventListeners()”removeAllEventListeners():
void
Remove all event listeners.
Returns
Section titled “Returns”void
Example
Section titled “Example”ngDiagramService.removeAllEventListeners();
removeEventListener()
Section titled “removeEventListener()”removeEventListener<
K
>(event
,callback?
):void
Remove an event listener.
Type Parameters
Section titled “Type Parameters”K
extends keyof DiagramEventMap
Parameters
Section titled “Parameters”K
The event name.
callback?
Section titled “callback?”EventListener
<DiagramEventMap
[K
]>
Optional specific callback to remove.
Returns
Section titled “Returns”void
Example
Section titled “Example”// Remove all listeners for an eventngDiagramService.removeEventListener('selectionChanged');
// Remove a specific listenerngDiagramService.removeEventListener('selectionChanged', myCallback);
setDefaultRouting()
Section titled “setDefaultRouting()”setDefaultRouting(
name
):void
Sets the default routing to use when not specified on edges.
Parameters
Section titled “Parameters”string
Name of the routing to set as default.
Returns
Section titled “Returns”void
setEventsEnabled()
Section titled “setEventsEnabled()”setEventsEnabled(
enabled
):void
Enable or disable event emissions.
Parameters
Section titled “Parameters”enabled
Section titled “enabled”boolean
Whether events should be emitted.
Returns
Section titled “Returns”void
Example
Section titled “Example”// Disable all eventsngDiagramService.setEventsEnabled(false);
// Re-enable eventsngDiagramService.setEventsEnabled(true);
startLinking()
Section titled “startLinking()”startLinking(
node
,portId
):void
Call this method to start linking from your custom logic.
Parameters
Section titled “Parameters”The node from which the linking starts.
portId
Section titled “portId”string
The port ID from which the linking starts.
Returns
Section titled “Returns”void
transaction()
Section titled “transaction()”transaction(
callback
):void
Executes a function within a transaction context. All state updates within the callback are batched and applied atomically.
Parameters
Section titled “Parameters”callback
Section titled “callback”() => void
Returns
Section titled “Returns”void
Example
Section titled “Example”this.ngDiagramService.transaction(() => { this.ngDiagramModelService.addNodes([node1, node2]); this.ngDiagramModelService.addEdges([edge1]);});
unregisterMiddleware()
Section titled “unregisterMiddleware()”unregisterMiddleware(
name
):void
Unregister a middleware from the chain.
Parameters
Section titled “Parameters”string
Name of the middleware to unregister.
Returns
Section titled “Returns”void
unregisterRouting()
Section titled “unregisterRouting()”unregisterRouting(
name
):void
Unregisters a routing implementation.
Parameters
Section titled “Parameters”string
Name of the routing to unregister.
Returns
Section titled “Returns”void
updateConfig()
Section titled “updateConfig()”updateConfig(
config
):void
Updates the current configuration.
Parameters
Section titled “Parameters”config
Section titled “config”Partial
<NgDiagramConfig
>
Partial configuration object containing properties to update.
Returns
Section titled “Returns”void