1

I need some reference for angular drag and drop, I went through the angular documentation but it's not working, I need to develop a page where I can drag tools to my canvas in angular. If anyone knows this please help.

Ananthakrishna
  • 517
  • 5
  • 24

2 Answers2

2

you need to add @angular/cdk and it is supporting packages .

working demo

Then include a module called DragDropModule in app.module

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { DragDropModule } from '@angular/cdk/drag-drop';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';

@NgModule({
  imports:      [ BrowserModule, FormsModule ,DragDropModule],
  declarations: [ AppComponent, HelloComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

in app.component.css

* { margin:0; padding:0; } /* to remove the top and left whitespace */

html, body { width:100%; height:100%; } /* just to be sure these are full screen*/

canvas { display:block; background-color: red } /* To remove the scrollbars */

lastly in app.component.html

<canvas #ele id="canvas" ></canvas>
  <div draggable="true" (dragend)="create()">
  Drag 
</div>

in app.component.ts

import { Component, VERSION,ViewChild,ViewContainerRef,ComponentFactoryResolver } from '@angular/core';
import { DraggableComponent } from './components/dragg.component';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular ' + VERSION.major;
      componentRef: any;
      count=0;
  @ViewChild('ele', { read: ViewContainerRef }) entry: ViewContainerRef;

  constructor(private resolver: ComponentFactoryResolver){

  }
  create(){
    
const factory = this.resolver.resolveComponentFactory(DraggableComponent);
        this.componentRef = this.entry.createComponent(factory);
        this.count++;
        this.componentRef.instance.text = "Draggble" +this.count;
  }
}
AhammadaliPK
  • 3,448
  • 4
  • 21
  • 39
1

You can get a reference from here drag-drop. I generally use this tool for drag and drop. Make sure you will get a solution.

After that, if you get any error then write it down over here.