【问题标题】:Is there any way to access the current index in the CdkDragMove event object有什么方法可以访问 CdkDragMove 事件对象中的当前索引
【发布时间】:2021-04-07 21:51:13
【问题描述】:

我正在使用 Angular CDK 拖放(Material UI),我想在 CdkDragMove 事件对象中获取被拖动项目的当前索引。谁能指导我?

【问题讨论】:

    标签: angular angular-material angular-cdk-drag-drop


    【解决方案1】:

    如果您使用*ngFor 迭代列表,您可以使用index 变量NgFor 上下文。

    <div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
      <div class="example-box" *ngFor="let movie of movies; let i = index" 
      (cdkDragMoved)="move($event, i)" cdkDrag>{{movie}}</div>
    </div>
    

    【讨论】:

    • 它只返回前一个索引,当我移动到新索引时不会更新。例如,如果您在 move 方法中添加 console.log(i),它会始终记录相同的值
    • 您可以在cdkDropList 上使用(cdkDropListSorted) 并在CdkDragSortEvent 对象上使用currentIndexpreviousIndex
    • 请考虑用真实答案更改答案。
    猜你喜欢
    • 1970-01-01
    • 2018-11-30
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多