Skip to content

Properties Sidebar Example

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 in ngDiagram.

import '@angular/compiler';
import { Component } from '@angular/core';
import {
initializeModel,
NgDiagramBackgroundComponent,
NgDiagramComponent,
provideNgDiagram,
type NgDiagramConfig,
} from 'ng-diagram';
import { SidebarContainer } from './sidebar/sidebar.component';
@Component({
imports: [NgDiagramComponent, NgDiagramBackgroundComponent, SidebarContainer],
providers: [provideNgDiagram()],
template: `
<div class="not-content diagram">
<ng-diagram [model]="model" [config]="config">
<ng-diagram-background />
</ng-diagram>
</div>
<sidebar-container />
`,
styleUrls: ['./diagram.component.scss'],
})
export class DiagramComponent {
config = {
zoom: {
zoomToFit: {
onInit: true,
padding: [50, 315, 50, 50],
},
},
} satisfies NgDiagramConfig;
model = initializeModel({
nodes: [
{
id: '1',
position: { x: 50, y: 120 },
data: { label: 'Node 1' },
rotatable: true,
},
{ id: '2', position: { x: 300, y: 120 }, data: { label: 'Node 2' } },
],
edges: [
{
id: '1',
source: '1',
sourcePort: 'port-right',
targetPort: 'port-left',
target: '2',
data: {},
},
],
});
}
  • Selection Tracking: Uses NgDiagramSelectionService.selection() to track selected nodes.
  • Computed Properties: Angular computed signals automatically update the UI when node properties change.
  • 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.