简体   繁体   中英

Angular drag-drop

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.

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.

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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM