Skip to content

NgDiagramModelService

since v0.8.0

The NgDiagramModelService provides methods for accessing and manipulating the diagram’s model.

private modelService = inject(NgDiagramModelService);
// Add nodes
this.modelService.addNodes([node1, node2]);
  • NgDiagramBaseService
  • OnDestroy

readonly edges: Signal<Edge<object>[]>

Readonly signal of current edges in the diagram.


readonly metadata: Signal<Metadata<object>>

Readonly signal of current diagram metadata.


readonly nodes: Signal<Node[]>

Readonly signal of current nodes in the diagram.

addEdges(edges, options?): Promise<void>

Adds new edges to the diagram.

Edge<object>[]

Array of edges to add.

Optional settings. Set waitForMeasurements: true to resolve only after the added elements (e.g. edge labels) have been measured. Available since 1.3.0.

boolean

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


addNodes(nodes, options?): Promise<void>

Adds new nodes to the diagram.

Node[]

Array of nodes to add.

Optional settings. Set waitForMeasurements: true to resolve only after the added nodes have been measured — useful before calling zoomToFit() or centerOnNode(). Available since 1.3.0.

boolean

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


attachEdge(edgeId, end, nodeId, portId?): Promise<boolean>

Attaches one endpoint of an edge to a node and, optionally, to a port. This is the opposite of detachEdge.

The same checks as for a relink drop apply: the node must exist and be visible, and the port must exist, be visible and have the right direction. The connection is then validated with linking.validateConnection, which receives the attached node as source or target according to end, and a context with reason: 'attach'.

string

ID of the edge to attach.

EdgeEnd

Which endpoint to attach.

string

ID of the node to attach to.

string

ID of the port to attach to. When omitted, the endpoint is attached to the node without a port.

Promise<boolean>

Whether the connection was valid and applied.

1.4.0


computePartsBounds(nodes, edges): Rect

Node[]

Array of nodes

Edge<object>[]

Array of edges

Rect

Bounding rectangle containing all visible nodes and edges. When there is nothing to measure (both arrays are empty, no visible node has measuredBounds, and no visible edge has points), returns a zero-size rectangle at the origin ({ x: 0, y: 0, width: 0, height: 0 }).

0.9.0

Computes the axis-aligned bounding rectangle that contains all specified nodes and edges.

Node bounds come from measuredBounds, which includes the measured ports and the rotation, not from the raw position and size in the model. Nodes must therefore already be measured (rendered), and the result can be larger than the node rectangles because of the ports. Edges contribute their routed points and their measured labels.

Since 1.4.0, effectively hidden elements (computedHidden) are left out, so their old geometry does not enlarge the result. If all given elements are hidden, the result is the same zero-size rectangle at the origin as for unmeasured elements.


deleteEdges(ids): Promise<void>

Deletes edges by their IDs.

string[]

Array of edge IDs to delete.

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


deleteNodes(ids): Promise<void>

Deletes nodes by their IDs.

Deleting a group also deletes all of its descendants (children, grandchildren, and so on). Edges connected to any deleted node are removed as well. This is the same behavior as deleting the selection.

string[]

Array of node IDs to delete.

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


detachEdge(edgeId, end, position?): Promise<void>

Detaches one endpoint of an edge, so that it becomes a free (dangling) endpoint.

When position is omitted, the endpoint stays anchored where it is now: at the current position of the port when the edge was connected to a port, otherwise at the routed endpoint of the edge.

Requires danglingEdges.enabled. With the feature off, this method does nothing and logs a console warning.

string

ID of the edge to detach.

EdgeEnd

Which endpoint to detach.

Point

Optional anchor position for the freed endpoint, in flow coordinates.

Promise<void>

1.4.0


getChildren<T>(groupId): Node<T>[]

Gets all children nodes for a given group node id

T extends object = object

The type of the nodes’ data property. Defaults to DataObject.

string

group node id

Node<T>[]

Array of child nodes


getChildrenNested<T>(groupId): Node<T>[]

Gets all nested children (descendants) of a group node

T extends object = object

The type of the nodes’ data property. Defaults to DataObject.

string

Group node id

Node<T>[]

Array of all descendant nodes (children, grandchildren, etc.)


getConnectedEdges<T>(nodeId): Edge<T>[]

Gets all edges connected to a node

T extends object = object

The type of the edges’ data property. Defaults to DataObject.

string

Node id

Edge<T>[]

Array of edges where the node is either source or target


getConnectedNodes<T>(nodeId): Node<T>[]

Gets all nodes connected to a node via edges

T extends object = object

The type of the nodes’ data property. Defaults to DataObject.

string

Node id

Node<T>[]

Array of nodes connected to the given node


getDanglingEndpoints(): DanglingEndpoint[]

Returns the free (unconnected) endpoints of all edges in the model. A dual dangling edge gives two entries. Temporary and effectively hidden edges are skipped.

DanglingEndpoint[]

The free endpoints with their edge, end and anchor position.

1.4.0


getEdgeById<T>(edgeId): null | Edge<T>

Gets an edge by id.

T extends object = object

The type of the edge’s data property. Defaults to DataObject.

string

Edge id.

null | Edge<T>

Edge or null if not found.


getModel(): ModelAdapter

Returns the current model that NgDiagram instance is using. Returns null if flowCore is not initialized.

ModelAdapter


getNearestDanglingEndpointInRange(point, range): null | DanglingEndpoint

Finds the free edge endpoint nearest to a point within a range. It works like getNearestPortInRange, but for the free endpoints of dangling edges. Temporary and effectively hidden edges are skipped.

Point

Point to check from.

number

Range to check in.

null | DanglingEndpoint

Nearest free endpoint in range, or null.

1.4.0


getNearestNodeInRange<T>(point, range): null | Node<T>

Gets the nearest node in a range from a point.

T extends object = object

The type of the node’s data property. Defaults to DataObject.

Point

Point to check from.

number

Range to check in.

null | Node<T>

Nearest node in range or null.


getNearestPortInRange(point, range): null | Port

Gets the nearest port in a range from a point.

Point

Point to check from.

number

Range to check in.

null | Port

Nearest port in range or null.


getNodeById<T>(nodeId): null | Node<T>

Gets a node by id.

T extends object = object

The type of the node’s data property. Defaults to DataObject.

string

Node id.

null | Node<T>

Node or null if not found.


getNodeEnds<S, T>(edgeId): null | { source: Node<S>; target: Node<T>; }

Gets the source and target nodes of an edge

S extends object = object

The type of the source node’s data property. Defaults to DataObject.

T extends object = object

The type of the target node’s data property. Defaults to DataObject.

string

Edge id

null | { source: Node<S>; target: Node<T>; }

Object containing source and target nodes, or null if edge doesn’t exist


getNodesInRange<T>(point, range): Node<T>[]

Gets all nodes in a range from a point.

T extends object = object

The type of the nodes’ data property. Defaults to DataObject.

Point

Point to check from.

number

Range to check in.

Node<T>[]

Array of nodes in range.


getOverlappingNodes<T>(nodeId): Node<T>[]

Detects collision with other nodes by finding all nodes whose rectangles intersect with the specified node’s bounding rectangle.

T extends object = object

The type of the nodes’ data property. Defaults to DataObject.

string

The ID of the node to check for collisions

Node<T>[]

An array of Nodes that overlap with the specified node

getOverlappingNodes<T>(node): Node<T>[]

T extends object = object

The type of the nodes’ data property. Defaults to DataObject.

Node<T>

The node to check for collisions

Node<T>[]

An array of Nodes that overlap with the specified node

0.9.0

Detects collision with other nodes by finding all nodes whose rectangles intersect with the specified node’s bounding rectangle.


getParentHierarchy<T>(nodeId): GroupNode<T>[]

Gets the full chain of parent group Nodes for a given nodeId.

T extends object = object

The type of the group nodes’ data property. Defaults to DataObject.

string

Node id

GroupNode<T>[]

Array of parent group Node objects, from closest parent to farthest ancestor


isNestedChild(nodeId, groupId): boolean

Checks if a node is a nested child (descendant) of a group node

string

Node id

string

Group node id

boolean

True if the node is part of the group’s nested subgraph


toJSON(): string

Serializes the current model to a JSON string.

string

The model as a JSON string.


updateEdge(edgeId, edge, options?): Promise<void>

Updates the properties of an edge.

string

Edge id.

Partial<Edge>

New edge properties.

Optional settings. Set waitForMeasurements: true to resolve only after measurements triggered by the update have completed. Available since 1.3.0.

boolean

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


updateEdgeData<T>(edgeId, data, options?): Promise<void>

Updates the data of an edge.

T extends object = object

The type of the edge’s data property. Defaults to DataObject.

string

Edge id.

T

New data to set for the edge.

Optional settings. Set waitForMeasurements: true to resolve only after measurements triggered by the update (e.g. re-rendered edge labels) have completed. Available since 1.3.0.

boolean

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


updateEdges(edges, options?): Promise<void>

Updates multiple edges at once.

Pick<Edge<object>, "id"> & Partial<Edge<object>>[]

Array of edge updates (must include id and any properties to update).

Optional settings. Set waitForMeasurements: true to resolve only after measurements triggered by the update have completed. Available since 1.3.0.

boolean

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


updateNode(nodeId, node, options?): Promise<void>

Updates the properties of a node.

string

Node id.

Partial<Node>

New node properties.

Optional settings. Set waitForMeasurements: true to resolve only after measurements triggered by the update have completed. Available since 1.3.0.

boolean

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


updateNodeData<T>(nodeId, data, options?): Promise<void>

Updates the data of a node.

T extends object = object

The type of the node’s data property. Defaults to DataObject.

string

Node id.

T

New data to set for the node.

Optional settings. Set waitForMeasurements: true to resolve only after measurements triggered by the update (e.g. a template resized by the new data) have completed. Available since 1.3.0.

boolean

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.


updateNodes(nodes, options?): Promise<void>

Updates multiple nodes at once.

Pick<Node, "id"> & Partial<Node>[]

Array of node updates (must include id and any properties to update).

Optional settings. Set waitForMeasurements: true to resolve only after measurements triggered by the update have completed. Available since 1.3.0.

boolean

Promise<void>

A promise that resolves once the change has been applied to the model. Inside a transaction, the promise resolves right away and the change is applied when the transaction commits.