Add links dynamically like diagram.model.addNodeData for nodes

Is there any function for links like for nodes we have where in we can add node dynamically using diagram.model.addNodeData?
I need a function where I can add links dynamicallly between nodes which are in the form

  1. link.fromNode.data.id,
  2. link.fromPortId,
  3. link.toNode.data.id,
  4. link.toPortId

Calling GraphLinksModel.addLinkData? Or do I not understand what you are asking for?

Great Thanks that worked.
Not sure why if my dropped node id on diagram is number:1 and if I use to draw a link from this node
I need to pass fromPortId: -1 not as 1 simply why I need to prefix - in the fromPortId and in toPortId.
I use angular 2 TS

That sounds suspicious. I would check the values of portId that are on your nodes. Maybe something like:

myDiagram.nodes.each(n => n.ports.each(p => console.log(n.data.key + ": " + p.portId))

Oops a typo. Sorry.
Is not fromPortId. And toPortId
It is from: and to:
I need to prefix - in front of from: and to:

I tried console.log the values of the node data.
the node unique id:came out to be 1 while adding to node data of diagram. But when i do a console.log on link drawn event the from: data came out to be -1 in the link data.
Any clue why?

No. Have you set Model.nodeKeyProperty to “id”?

Did you set id: 1 in that node data? Because by default that would not be a key value that the Model would assign. Whereas -1 does seem like an automatically generated key value.

Does myDiagram.findNodeForKey(1) return the Node that you think you are working with, the Node whose port was the source of the LinkingTool operation?