Angular 7 次移动数组中的对象导致 -1 索引,不会保存位置

Angular 7 move objects in array results in -1 index, will not save position

所以我在任务板上工作,我正在制作一个按钮,将所选任务移动到该容器的 top/bottom。它工作正常,但没有保存位置,当我重新加载时它处于以前的位置。我注意到当我这样做时,数组末尾有一个 -1 索引。我在网上搜索过这个问题,但找不到任何类似的东西。

这是我的函数代码:

public moveToBottom(container, index) {
this.container = container;
var lastIndex = (this.container.length -1);
this.loader.show();
transferArrayItem(this.container, this.container, index, lastIndex);

// this.verifyOrder(lastIndex);
this.loader.hide();
console.log(index, lastIndex, container)

我认为它与 container.length -1 有关,但这是我所看到的获取最后一个索引的唯一方法。我只需要索引的编号,而不是实际的对象。

如有任何帮助,我们将不胜感激!谢谢!

你只需要使用 transferArrayItem 功能。

例如,有一个电影数组this.movies。 因此,按钮点击功能将如下所示:

moveToTheEnd(index) {
    moveItemInArray(this.movies, index, this.movies.length - 1);
}

和HTML:

<div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
  <div *ngFor="let movie of movies; let index = index" style="display: flex; align-items: center">
      <div class="example-box"  cdkDrag>{{movie}}</div>
      <div (click)="moveToTheEnd(index)" style="background: red">Move to the end</div>
  </div>
</div>

示例如下:https://stackblitz.com/edit/angular-vkwb8v