【问题标题】:Angular drag-drop角度拖放
【发布时间】:2020-06-29 09:55:26
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    你需要添加@angular/cdk,它是支持包。

    working demo

    然后在app.module 中包含一个名为DragDropModule 的模块

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

    【讨论】:

    • 感谢提供信息,但我需要拖动复制品,就像我们设计徽标时可以拖放工具一样,实际上我们正在移动复制品吗?
    • 您可以将cdkDrag添加到任何元素以使其可拖动
    • 请使用此链接获取更多信息,medium.com/angular-in-depth/…
    • 我已经看到了,但是我不需要移动整个
      我只需要移动副本。即,就像在油漆中拖动剪贴画一样。你能用它跟我来吗?
    • 你能加入这个chat
    【解决方案2】:

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

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

    【讨论】:

    • 我首先做了同样的事情,但它不起作用。 this 是我提到的例子,但事情不会拖累。
    • 你的意思是,你需要拖出虚线对吗?
    • 我想把它拖到盒子里,但它不会被移动。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签