我正在尝试使用 CdkDragDrop 重新排序托管在 CdkPortal 窗口中的列表,但它不起作用。
HTML:
<ng-container *cdkPortal>
<ng-content></ng-content>
<hr />
<div cdkDropList class="example-list mt-20"
(cdkDropListDropped)="drop($event)">
<div class="example-box" *ngFor="let movie of movies, index as i"
cdkDrag>
<div class="drag-index" (click)="writelog(movie.name)">{{movie.name}}
</div>
</div>
</div>
<p>{{movie | json}}</p>
打字稿:
import { CdkDragDrop, moveItemInArray } from "@angular/cdk/drag-drop";
import { Component } from "@angular/core";
@Component({
selector: "my-app",
styleUrls: ["./app.component.css"],
templateUrl: "./app.component.html"
})
export class AppComponent {
showPortal = false;
movies = [
{
name: "Episode I - The Phantom Menace",
isDisable: false
},
{
name: "Episode II - Attack of the Clones",
isDisable: false
},
{
name: "Episode III - Revenge of the Sith",
isDisable: false
},
{
name: "Episode IV - A New Hope",
isDisable: false
},
{
name: "Episode V - The Empire Strikes Back",
isDisable: false
},
{
name: "Episode VI - Return of the Jedi",
isDisable: false
}
];
drop(event: CdkDragDrop<string[]>) {
console.log(event.currentIndex + "|||" + event.previousIndex);
moveItemInArray(this.movies, event.previousIndex, event.currentIndex);
}
}
这是显示问题的 StackBlitz。
https://stackblitz.com/edit/angular-open-window-g5stn3?file=src%2Fapp%2Fwindow.component.ts
在页面中,您可以看到一个列表...此列表可以通过拖放重新排序。
现在单击“打开我”按钮,然后尝试拖动以重新排序相同的列表(我无法获得其中的样式,但它是相同的代码)它不会重新排序。