So I have this child component, where the id
property is set randomly :
export class FileSelectionComponent implements AfterViewInit {
public type = 'app-file-selection';
public id = 'FileSelection#' + Math.random().toString(16).slice(2, 8);
@Input() jspinst: any;
public x;
public y;
public selected_file: string;
constructor(public dialog: MatDialog, private httpClient: HttpClient) {
this.selected_file = '';
console.log('constructor called');
console.log(this.id);
}
ngAfterViewInit() {
...
this.jspinst.addEndpoint(this.id, { anchor: 'Right'}, Endpoint1);
this.jspinst.addEndpoint(this.id, { anchor: 'Left'}, Endpoint2);
this.jspinst.draggable(this.id);
}
}
and the parent component goes like this:
export class FlowComponent implements OnInit, AfterViewInit, OnChanges {
public nodes: Array<any>;
public jspl;
constructor() {
this.nodes = [];
this.jspl = jsPlumb.getInstance();
}
addNode(type) {
let nn = new FileSelectionComponent();
this.nodes = this.nodes.concat([nn]);
s = this.nodes;
console.log('object created and added');
}
ngAfterViewInit() {
s = this.nodes;
this.jspl.bind('connection', function (info) {
console.log(info.sourceId+' ----> '+info.targetId); //this output
console.log(s[0].id+' ----> '+s[1].id); // and this output are not the same while they should
console.log(Object.keys(s[0]));
console.log(Object.values(s[0]));
});
}
}
the addNode
methode is called when I click a button, and as you can guess the constructor for the FileSelectionComponent
is called twice, generating two different ids, which making it impossible for me retrieve the concerned nodes when the connection
event is fired.
I found some similar questions like this one, but none helped:
- My button have a
type="button"
on it - I'm not bootstrapping both parent and child component, actually I'm not bootstrapping any of them, I already checked the
app.module.ts
(I don't even know bootstrapping is about). - I didn't forget to close the selector tag in the host component.
- I don't have a
platformBrowserDynamic().bootstrapModule(AppModule);
in my code. - the compiler doesn't show any error messages.
the Template for the parent goes like this:
<div id="cont">
<div *ngFor="let n of nodes">
<app-file-selection [jspinst]="jspl" *ngIf="n.type === 'app-file-selection'"></app-file-selection>
</div>
</div>
<button type="button" mat-icon-button matTooltip="Files selection" (click)="addNode('file-selection')"><mat-icon aria-label="Side nav toggle icon">insert_drive_file</mat-icon></button>
I know this kind of questions was asked over and over, but my searchs don't seem to be helpful, thanks in advance.
EDIT I already tried placing that random assignement inside the constructor (resulting in two ids one caught by jsplumb and one received by the parent component), and placing it inside the OnInit (results in just one id but not known by the parent component).