How to create and update annotation template dynamically in Angular Diagram
In Angular Diagram, Annotation is a block of text that can be displayed over a node or connector. Annotation is used to textually represent an object with a string that can be edited at runtime. Diagram provides either an HTML or SVG template to node and connector annotations. To do this, simply access the annotation template property and specify the desired template. The template should be in a string format. To see an example of how to set a annotation template for a node and connector, please refer to the code snippet below.
//initilaize the diagram
<ejs-diagram #diagram id="diagram" width="100%" height="780px"
[nodes]="nodes" [connectors]="connectors" > </ejs-diagram>
//initilaize the nodes and connectors
public nodes: NodeModel[] = [
{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
annotations:[ {
content: '|||',
template:
'<div><img src = "https://www.freepnglogos.com/uploads/plus-icon/plus-icon-plus-math-icon-download-icons-9.png" height = "30" weight = "30"/></div>',
},]
}, ]
public connectors: ConnectorModel[] = [
{
id: 'connector4',
sourcePoint: { x: 250, y: 400 },
targetPoint: { x: 400, y: 400 },
annotations: [
{
content: 'F',
offset: 1,
margin: { bottom: 20 },
template: '<div><input type="button" value="Submit"></div>',
},
{
offset: 0.5,
content: 'HTML Template',
margin: { top: 20 },
}
],
},
];
The provided code sets initial annotation templates for a node and a connector.
Additionally, annotation templates can be changed dynamically in response to user actions. The code snippet below illustrates how to update the annotation template for a node and a connector on a button click:
<button (click)="updateTemplate()">Update template</button>
public updateTemplate () {
// Set the annotation template for node and connector annotation this.diagram.connectors[0].annotations[0].template = '<div><input type="button" value="hello"></div>';// To reflect the changes in diagram this.diagram.dataBind(); }this.diagram.nodes[0].annotations[0].template = '<div><input type="button" value="Button"></div>';
This code dynamically changes the annotation templates for a node and a connector, followed by updating the diagram to reflect the changes.
For a live demonstration, please refer to the provided sample showcasing the use of annotation templates for nodes and connectors.