DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

How to Add Drag and Drop to an Angular App with CDK

Angular CDK supplies drag interactions, but your drop handler must update the backing data. Learn setup, sorting, connected lists, customization, and scrolling.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Angular CDK provides the drag interactions and visual sorting for lists; your application must update its data when a drop occurs. Install @angular/cdk, mark the list with cdkDropList and its items with cdkDrag, then handle cdkDropListDropped to keep the displayed order and backing data aligned.

Install and set up Angular CDK drag and drop

The drag-and-drop package is @angular/cdk/drag-drop. Angular’s guide shows adding the CDK with the Angular CLI, then importing the directives used by the component. See the Angular drag-and-drop guide for version-appropriate setup details.

As an Amazon Associate I earn from qualifying purchases.

  1. From your project directory, run ng add @angular/cdk.

    What’s actually slowing this PC down?

    Pick the symptom - the matching free tool is one click away.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  2. In the component that owns the list, import CdkDropList and CdkDrag from @angular/cdk/drag-drop, using the import style supported by your Angular project.

  3. Put cdkDropList on the list container and cdkDrag on each item. Bind the list’s (cdkDropListDropped) event to a component handler.

Angular’s current guide and API pages do not establish a release number in the captured content, so check the documentation matching the version installed in your project before relying on version-specific syntax.

Build a sortable list and update its data

A minimal standalone component can look like this. The key line is the call to moveItemInArray in the drop handler: CDK changes the interaction and visual sorting, but it does not reorder the application’s data model for you.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import { Component } from '@angular/core';
import {
  CdkDrag,
  CdkDropList,
  CdkDragDrop,
  moveItemInArray,
} from '@angular/cdk/drag-drop';

@Component({
  selector: 'app-sortable-list',
  standalone: true,
  imports: [CdkDropList, CdkDrag],
  template: `
    <ul cdkDropList (cdkDropListDropped)="drop($event)">
      @for (item of items; track item) {
        <li cdkDrag>{{ item }}</li>
      }
    </ul>
  `,
})
export class SortableListComponent {
  items = ['First', 'Second', 'Third'];

  drop(event: CdkDragDrop<string[]>) {
    moveItemInArray(this.items, event.previousIndex, event.currentIndex);
  }
}

The example uses Angular’s modern control-flow syntax; projects using a different template style can keep the same CDK directives and drop-handler logic while following their installed Angular version’s syntax. The event provides previousIndex and currentIndex, which identify the item’s old and new positions in the list. The CdkDragDrop API also documents the source and destination containers, pointer-over-container status, pointer distance and drop point, and the native mouse or touch event.

Move items between connected lists

Use cdkDropListConnectedTo when the destination lists are known, referencing another drop-list instance or its ID. If lists are created dynamically or the set is not known in advance, put them under cdkDropListGroup to connect the group automatically. An optional cdkDropListEnterPredicate can decide whether an item may enter a destination.

Connections permit drag-and-drop between lists; they do not transfer items in your application’s collections. Handle the event by using its previousContainer and container to identify the source and destination, then remove the item from the source array and insert it into the destination array. For example:

import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';

drop(event: CdkDragDrop<string[]>) {
  if (event.previousContainer === event.container) {
    moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
  } else {
    transferArrayItem(
      event.previousContainer.data,
      event.container.data,
      event.previousIndex,
      event.currentIndex,
    );
  }
}

For this handler, bind each list’s backing collection through its cdkDropListData input so the event containers expose the arrays being updated. The Angular guide covers connected lists, groups, and list data handling.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose sorting, movement, and acceptance behavior

Angular CDK exposes several independent controls; use only the ones your interface needs. The CdkDropList API describes list-level configuration, while the CdkDrag API describes item-level options.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Enable automatic scrolling in a scrollable container

If automatic scrolling fails while dragging items inside a scrollable element, add cdkScrollable to that container. The CDK needs the directive to detect and control the container’s scrolling during a drag; the official guide explains this requirement in its scrollable-container guidance.

What to implement for each use case

Need CDK setup Application responsibility
Reorder one list cdkDropList, cdkDrag Reorder the array with moveItemInArray using the event indices.
Move items among a known set of lists Connect lists with cdkDropListConnectedTo. Remove from the source collection and insert into the destination collection.
Move items among dynamic lists Group lists with cdkDropListGroup. Update the relevant backing collections on drop.
Allow only certain destinations or positions Use entry or sort predicates. Keep the data update consistent with the accepted drop.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.