Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在具有角度CDK拖动的拖动元素上丢失旋转位置_Angular_Angular Material_Drag And Drop_Rotatetransform_Angular Cdk Drag Drop - Fatal编程技术网

Angular 在具有角度CDK拖动的拖动元素上丢失旋转位置

Angular 在具有角度CDK拖动的拖动元素上丢失旋转位置,angular,angular-material,drag-and-drop,rotatetransform,angular-cdk-drag-drop,Angular,Angular Material,Drag And Drop,Rotatetransform,Angular Cdk Drag Drop,我尝试使用范围滑块旋转一个元素,但每次拖动该元素时,它都会在拖动时丢失旋转值,并且还会更改长方体的位置并将其放置在起点。 我附加了一个链接到我在stackblitz中创建的一个简单测试项目,这样我就可以重新创建我遇到的问题 有人能指导我解决这个问题吗 我将代码放在这里,以防有人无法很好地工作到测试项目的链接: 应用程序模块.ts import { NgModule } from "@angular/core"; import { BrowserModule } from &

我尝试使用范围滑块旋转一个元素,但每次拖动该元素时,它都会在拖动时丢失旋转值,并且还会更改长方体的位置并将其放置在起点。 我附加了一个链接到我在stackblitz中创建的一个简单测试项目,这样我就可以重新创建我遇到的问题

有人能指导我解决这个问题吗

我将代码放在这里,以防有人无法很好地工作到测试项目的链接:

应用程序模块.ts

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 {}
import { Component, Renderer2 } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  rotateValue = 0;
  dragPosition = { x: 0, y: 0 };

  constructor(private renderer: Renderer2) {}

  setRotate(value: string) {
    this.rotateValue = Number(value);
    this.renderer.setStyle(
      document.querySelector(".example-box"),
      "transform",
      `rotate(${this.rotateValue}deg)`
    );
  }
}
.example-box {
  width: 140px;
  height: 140px;
  border: solid 1px #ccc;
  color: rgba(0, 0, 0, 0.87);
  cursor: move;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  background: #fff;
  border-radius: 4px;
  margin-right: 25px;
  position: relative;
  z-index: 1;
  box-sizing: border-box;
  padding: 10px;
  transition: box-shadow 200ms cubic-bezier(0, 0, 0.2, 1);
  box-shadow: 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 2px 2px 0 rgba(0, 0, 0, 0.14),
    0 1px 5px 0 rgba(0, 0, 0, 0.12);
}

.example-box:active {
  box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
    0 8px 10px 1px rgba(0, 0, 0, 0.14), 0 3px 14px 2px rgba(0, 0, 0, 0.12);
}

.example-boundary {
  width: 300px;
  height: 500px;
  max-width: 100%;
  border: dotted #ccc 2px;
}
应用程序组件.ts

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 {}
import { Component, Renderer2 } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  rotateValue = 0;
  dragPosition = { x: 0, y: 0 };

  constructor(private renderer: Renderer2) {}

  setRotate(value: string) {
    this.rotateValue = Number(value);
    this.renderer.setStyle(
      document.querySelector(".example-box"),
      "transform",
      `rotate(${this.rotateValue}deg)`
    );
  }
}
.example-box {
  width: 140px;
  height: 140px;
  border: solid 1px #ccc;
  color: rgba(0, 0, 0, 0.87);
  cursor: move;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  background: #fff;
  border-radius: 4px;
  margin-right: 25px;
  position: relative;
  z-index: 1;
  box-sizing: border-box;
  padding: 10px;
  transition: box-shadow 200ms cubic-bezier(0, 0, 0.2, 1);
  box-shadow: 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 2px 2px 0 rgba(0, 0, 0, 0.14),
    0 1px 5px 0 rgba(0, 0, 0, 0.12);
}

.example-box:active {
  box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
    0 8px 10px 1px rgba(0, 0, 0, 0.14), 0 3px 14px 2px rgba(0, 0, 0, 0.12);
}

.example-boundary {
  width: 300px;
  height: 500px;
  max-width: 100%;
  border: dotted #ccc 2px;
}
app.component.html

<h1 class="text-center m-3">Drag And Drop Project</h1>
<hr>

<div class="row m-5">

    <div class="col-sm-7">
        <div class="example-boundary">
            <div class="example-box" cdkDragBoundary=".example-boundary" cdkDrag>
                I can only be dragged within the dotted container
            </div>
        </div>
    </div>

    <div class="col-sm-5">
        <h4>SETTINGS</h4>
        <ul class="list-group mb-3">
            <li class="list-group-item d-flex justify-content-between lh-condensed">
                <div>
                    <h6 class="my-0">Rotate the box</h6>
                    <input #rotation
                   type="range"
                   class="custom-range  my-2"
                    min="-150" max="150"
                    [(ngModel)]="rotateValue"
                   [value]='rotateValue'
                   (change)="setRotate(rotation.value)"
                  >
        </div>
                    <span id="grados" class="text-muted">{{rotateValue}}º</span>
            </li>

        </ul>
    </div>
</div>

非常感谢您

您遇到的问题是,Cdk拖动是使用CSS
transform
规则实现的,与您的自定义旋转完全相同。 因此,当应用于完全相同的HTML元素时,这两者基本上是不兼容的。最后一个操作(拖动或旋转)将覆盖另一个操作

在我看来,最简单的解决方法是在可拖动的包装器中包装旋转的元素

以下是更新的StackBlitz:

编辑的摘要:

在模板中,我用一个draggable(我也使用
[ngStyle]
来包装可旋转的div,并完全避免直接的DOM操作,这本身不是问题,但是不必要的):

组件被清除:

import { Component } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  rotateValue = 0;
  dragPosition = { x: 0, y: 0 };

  constructor() {}

  setRotate(value: string) {
    this.rotateValue = +value;
  }
}

好吧,我想你已经猜到了,拖动动作会弄乱长方体的样式,每次拖动长方体时,之前的旋转值都会丢失。但是,同时,您也在搞乱长方体样式,使用新的旋转值覆盖其当前值,因此每次旋转长方体时(删除
translate3D
值),长方体位置都会不断重置

您可以通过获取元素的当前样式并在将其放回之前将其与您的样式合并来轻松修复代码,这将修复位置问题,但再次拖动长方体时仍会丢失旋转值

好消息是这个bug已经有了一个和一个,但坏消息是它们仍然是开放的