Angular Material 10芯片列表跨拖放空列表占位符异常咨询
问题根源
Angular Material 10 中mat-chip-list的实际渲染结构为:
- 根节点是
<mat-chip-list>元素 - 根节点内部自动生成一个类名为
.mat-chip-list-wrapper的块级容器 - 所有
<mat-chip>芯片元素最终都会被投影渲染在这个wrapper容器内部
原配置将cdkDropList系列拖放指令直接绑定在<mat-chip-list>根元素上,而CDK拖放逻辑的占位符插入规则为:将拖拽占位符作为绑定了cdkDropList的容器的直接子元素插入。当目标列表为空时,wrapper内部没有芯片作为位置参考,CDK会把占位符直接插入到<mat-chip-list>根节点下,成为wrapper的同级元素,最终出现占位符悬空在预留空白区域下方的问题。
调整方案
核心思路是指定CDK拖拽占位符的插入位置,让占位符和真实芯片处于同一个DOM容器内,同时优化空列表的交互区域样式,避免非空列表出现多余空白。
步骤1:调整组件逻辑
在app.component.ts中获取两个芯片列表内部的wrapper元素,用于指定占位符的插入目标:
- 导入需要的依赖:
ViewChild, AfterViewInit, ChangeDetectorRef、MatChipList组件 - 新增类属性存储两个列表的wrapper DOM引用
- 在视图初始化完成后查询内部wrapper元素,触发变更检测保证模板绑定生效
调整后的组件代码:
import { Component, ViewChild, AfterViewInit, ChangeDetectorRef } from '@angular/core'; import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop'; import { MatChipList } from '@angular/material/chips'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.component.css' ] }) export class AppComponent implements AfterViewInit { primaryOptions = ['Apple', 'Carrot', 'Banana']; secondaryOptions = []; // 存储两个列表的wrapper元素引用 primaryWrapper!: HTMLElement; secondaryWrapper!: HTMLElement; // 获取模板中的mat-chip-list实例 @ViewChild('primaryChipList') primaryChipList!: MatChipList; @ViewChild('secondaryChipList') secondaryChipList!: MatChipList; constructor(private cdr: ChangeDetectorRef) {} ngAfterViewInit() { // 查询mat-chip-list内部自动生成的wrapper元素 this.primaryWrapper = this.primaryChipList._elementRef.nativeElement.querySelector('.mat-chip-list-wrapper'); this.secondaryWrapper = this.secondaryChipList._elementRef.nativeElement.querySelector('.mat-chip-list-wrapper'); // 手动触发变更检测,让模板绑定的占位符容器生效 this.cdr.detectChanges(); } 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 ); } } }
步骤2:调整模板配置
在app.component.html中:
- 给两个
mat-chip-list添加模板引用变量,方便ViewChild获取实例 - 给两个
cdkDropList绑定[cdkDropListPlaceholderContainer]输入属性,指定占位符要插入的wrapper元素位置
调整后的模板片段:
<div id='mainView'> <h3>Primary One</h3> <mat-chip-list #primaryChipList cdkDropList id='primary-list' [cdkDropListData]='primaryOptions' [cdkDropListConnectedTo]="['secondary-list']" [cdkDropListPlaceholderContainer]="primaryWrapper" (cdkDropListDropped)='drop($event)' class='mat-chip-list-stacked drag-area' aria-orientation="vertical"> <mat-chip *ngFor='let i of primaryOptions' cdkDrag [removable]="true" style='width: auto;'> {{i}} <mat-icon matChipRemove>cancel</mat-icon> </mat-chip> </mat-chip-list> <h3>Secondary List</h3> <mat-chip-list #secondaryChipList cdkDropList id='secondary-list' [cdkDropListData]='secondaryOptions' [cdkDropListConnectedTo]="['primary-list']" [cdkDropListPlaceholderContainer]="secondaryWrapper" (cdkDropListDropped)='drop($event)' class='mat-chip-list-stacked drag-area' aria-orientation="vertical"> <mat-chip *ngFor='let i of secondaryOptions' cdkDrag [removable]="true" style='width: auto;'> {{i}} <mat-icon matChipRemove>cancel</mat-icon> </mat-chip> </mat-chip-list> </div>
步骤3:优化CSS样式
原实现给所有列表的wrapper都加了最小高度和边框,会导致非空列表出现多余的视觉空白。调整CSS选择器,仅给空状态的wrapper添加最小高度,保证只有空列表有足够的拖放交互区域:
#mainView { padding: 10px; } .drag-area { border: solid 1px red; } /* 仅对空状态的wrapper添加交互区域样式 */ :host /deep/ .drag-area .mat-chip-list-wrapper:empty { border: solid 1px #ccc; border-radius: 4px; min-height: 36px; }
调整后效果
- 拖拽芯片悬浮在目标空列表上方时,占位符会直接插入到wrapper容器内部,和真实芯片处于同一DOM层级,不再出现悬空错位
- 空列表保留36px高度的拖放交互区域,非空列表完全还原芯片列表的原生视觉效果,无多余空白
- 放置完成后,真实芯片的插入位置和拖拽预览的占位符位置完全对齐,无跳动错位
内容的提问来源于stack exchange,提问作者Ian
相关产品推荐
相关产品推荐

