1

I have a generated div in which I'd like to add the drag&drop.

In HTML, it is something like this:

 (div) cdkdrag ... (/div)

But as my component is generated, i'd like to do something like that but don't know how:

let myDiv = document.createElement('div');
"myDiv.cdkDrag...?????"

If anyone know if it's possible and how ?

HTML way: <div cdkdrag >...</div>
Viktor
  • 2,623
  • 3
  • 19
  • 28
colin
  • 21
  • 2

1 Answers1

-1

I would suggest an easier option would be to have your template and use conditional *ngIf or *ngFor statements to construct/destruct. That way you will already have the correct attributes and have added checks during compile.

i.e

  <div *ngIf="shouldShowDragElement" cdkDrag>...</div>

or

  <div *ngFor="let element of generatedElementsList" cdkDrag>...</div>
chrismclarke
  • 1,995
  • 10
  • 16
  • Thanks chris, but unfortunately it could not work in my case because i have to create my dragable div in another generated component: let myDiv = document.createElement('div'); "myDiv.cdkDrag...?????" AnotherGeneratedDIV.appendChild(myDraggableDIV) The other way i've find for the moment is to create my div in the html and to move it in my generated div. But i will be better if i could done all in the ts dynamically. – colin Aug 27 '19 at 15:49
  • Ok, in that case you might want to take a look at the thread here: https://stackoverflow.com/questions/41298168/how-to-dynamically-add-a-directive/43752424 – chrismclarke Aug 27 '19 at 17:46