【发布时间】:2020-02-26 09:44:22
【问题描述】:
编辑 2:这个问题已经由了不起的 CDK 团队解决了!
编辑:由于这似乎是一种很奇怪的行为,我现在添加了an issue in the official CDK repository。不过,您可以随意提出任何想法或解决方法。
我的*cdkDropList 中有不同类型的元素,在拖动它们时需要不同的占位符高度。由于该数组具有属性为type 的对象,因此我将这些类型添加为CSS 类并尝试使用[ngClass] 动态添加它们。但是,这些动态生成的类的行为与我将它们设置为“常规”CSS 类时的行为不同。
当我动态设置类时会发生这种情况:
占位符和dropList 中的元素重叠。以下是相关代码:
example.component.ts
contentItems: ContentItem[] = [
{ type: 'text', /* more props */ },
{ type: 'text', /* more props */ },
{ type: 'image', /* more props */ }
];
example.component.html
<div *ngFor="let item of contentItems" class="editor-item" cdkDrag>
<div [ngClass]="['dropzone-placeholder', item.type]" *cdkDragPlaceholder>
<p>{{ 'EDITOR.INSERT HERE' | translate }}</p>
</div>
<app-language-tab-editor *ngIf="item.type === 'text'"></app-language-tab-editor>
<app-image-upload *ngIf="item.type === 'image'"></app-image-upload>
</div>
example.component.scss
$dropzone-placeholder-dark: #00973B;
$dropzone-placeholder-light: #00973B0D;
$text-placeholder-height: 135px;
$image-placeholder-height: 375px;
.dropzone-placeholder {
border: 1px dashed $dropzone-placeholder-dark;
color: $dropzone-placeholder-dark;
background: $dropzone-placeholder-light;
&.text {
height: $text-placeholder-height;
}
&.image {
height: $image-placeholder-height;
}
}
我目前只有两种不同的类型,但目标是使其易于扩展,以便以后添加更多。我也已经尝试过使用class="dropzone-placeholder {{ item.type }}" 和[class]="'dropzone-placeholder ' + item.type",但无济于事。
经过进一步测试我还发现,即使我们不使用变量,使用[ngClass] 通常也不起作用。使用[ngClass]="['dropzone-placeholder', 'text']" 也不起作用。
这是预期的行为:
dropList 中的占位符和元素不重叠,而是适当地放置在彼此下方。这种行为目前只能通过定期设置类来实现,但 HTML 看起来相当不愉快,因为将来代码会变得一团糟。
example.component.html
<div *ngFor="let item of contentItems" class="editor-item" cdkDrag>
<div *ngIf="item.type === 'text'">
<div class="dropzone-placeholder reorder text" *cdkDragPlaceholder>
<p>{{ 'EDITOR.INSERT HERE' | translate }}</p>
</div>
</div>
<div *ngIf="item.type === 'image'">
<div class="dropzone-placeholder reorder image" *cdkDragPlaceholder>
<p>{{ 'EDITOR.INSERT HERE' | translate }}</p>
</div>
</div>
<app-language-tab-editor *ngIf="item.type === 'text'"></app-language-tab-editor>
<app-image-upload *ngIf="item.type === 'image'"></app-image-upload>
</div>
为什么当类没有以传统方式设置时,CDK 的行为会有所不同?以及如何避免编写上面提到的冗余解决方法?
【问题讨论】:
标签: angular drag-and-drop angular-cdk angular-cdk-drag-drop