Skip to content

Sidebar with Editing Properties

Creating a sidebar for reading and updating node properties in ngDiagram is simple and requires minimal code. This example demonstrates how to build an interactive properties panel that automatically updates when nodes are selected and allows real-time editing of node attributes.

The sidebar is implemented using just a few key concepts:

  1. Selection Tracking: Uses NgDiagramSelectionService.selection() to track selected nodes
  2. Computed Properties: Angular computed signals automatically update the UI when node properties change
  3. Simple Update Methods: Direct calls to updateNodeData() and updateNode() for property modifications

The sidebar component automatically:

  • Observes the current selection using the model service
  • Displays the selected node’s properties (label, resizable, rotatable)
  • Updates the node in real-time when properties are changed
  • Disables controls when no node is selected

This pattern makes it easy to create property editors for any node attributes you need to expose to users.

import '@angular/compiler';
import { Component } from '@angular/core';
import {
initializeModel,
NgDiagramComponent,
provideNgDiagram,
} from 'ng-diagram';
import { SidebarContainer } from './sidebar.component';
@Component({
imports: [NgDiagramComponent, SidebarContainer],
providers: [provideNgDiagram()],
template: `
<ng-diagram [model]="model" />
<sidebar-container />
`,
styles: `
:host {
flex: 1;
display: flex;
height: 100%;
.coordinates {
display: flex;
}
}
`,
})
export class NgDiagramPropertiesSidebarContainer {
model = initializeModel({
metadata: {
viewport: { x: -45, y: 80, scale: 0.88 },
},
nodes: [
{
id: '1',
position: { x: 100, y: 150 },
data: { label: 'Node 1' },
rotatable: true,
},
{ id: '2', position: { x: 400, y: 150 }, data: { label: 'Node 2' } },
],
edges: [
{
id: '1',
source: '1',
sourcePort: 'port-right',
targetPort: 'port-left',
target: '2',
data: {},
},
],
});
}