角度拖放

时间:2020-06-29 09:55:26

标签: angular angular-material drag-and-drop angular-dragdrop

我需要一些有关角度拖放的参考,我浏览了角度文档,但是它不起作用,我需要开发一个页面,在其中我可以将工具以角度拖放到画布上。 如果有人知道这一点,请帮助。

2 个答案:

答案 0 :(得分:1)

您可以从这里drag-drop获得参考。我通常使用此工具进行拖放。确保您会找到解决方案。

在那之后,如果遇到任何错误,请在此处写下。

答案 1 :(得分:1)

您需要添加@angular/cdk,它正在支持软件包。

working demo

然后在DragDropModule中包含一个名为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 { }

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 */

最后在app.component.html

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

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