如何使用@ angular / cdk使NgbModal可拖动

时间:2019-10-17 14:59:17

标签: javascript html angular typescript draggable

在弄清楚如何使我的模态可拖动时,我遇到了一些困难。我有可重用的模态及其自身的服务,称为创建一个内部组件。

confirm.modal.service.ts

import { Injectable } from "@angular/core";
import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
import { Observable, from, EMPTY, throwError } from "rxjs";
import { catchError, tap } from "rxjs/operators";

import { ConfirmModalComponent } from "./confirm-modal.component";

export interface ConfirmOptions {
    title: string;
    subtitle?: string;
    errorOnClose?: boolean;
}

@Injectable({ providedIn: "root" })
export class ConfirmModalService {
    constructor(private modalService: NgbModal) {}

    confirm(options: ConfirmOptions): Observable<boolean> {
        const modalRef = this.modalService.open(ConfirmModalComponent, {
            centered: true
        });
        modalRef.componentInstance.title = options.title || "Are you sure?";
        modalRef.componentInstance.subtitle = options.subtitle || null;

        return from(modalRef.result).pipe(
            tap(),
            catchError(err =>
                options.errorOnClose
                    ? throwError(err || "not confirmed")
                    : EMPTY
            )
        );
    }
}

confirm.modal.module.ts

import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { DragDropModule } from "@angular/cdk/drag-drop";

import { ConfirmModalComponent } from "./confirm-modal.component";

@NgModule({
    imports: [
        CommonModule,
        DragDropModule
    ],
    declarations: [ConfirmModalComponent],
    exports: [ConfirmModalComponent]
})
export class ConfirmModalModule {}

confirm.modal.component.ts

import { Component, Input } from "@angular/core";
import { NgbActiveModal } from "@ng-bootstrap/ng-bootstrap";

@Component({
    selector: "app-confirm-modal",
    templateUrl: "./confirm-modal.component.html",
    styleUrls: ["./confirm-modal.component.scss"]
})
export class ConfirmModalComponent {
    @Input() title: string;
    @Input() subtitle: string;

    constructor(public activeModal: NgbActiveModal) {}

    public accept(): void {
        this.activeModal.close(true);
    }

    public dismiss(): void {
        this.activeModal.close(false);
    }
}

confirm.modal.component.html

<div class="modal-body">
    <div class="modal-body__header">
        <span>{{ title }}</span>
    </div>
    <div *ngIf="subtitle" class="modal-body__text">
        <span>{{ subtitle }}</span>
    </div>
    <div class="modal-body__button-row">
        <button class="btn btn-primary" (click)="accept()">Yes</button>
        <button class="btn btn-light" (click)="dismiss()">Cancel</button>
    </div>
</div>

因此,我想使用Angular built-in DragDropModule拖动整个模式,因此我应该在cdkDrag的元素内添加class='modal-content',但是我不知道如何使用当前设置来实现。 NgbModalOptions提供仅添加类但不添加属性指令的功能。 我知道JQuery可拖动有更简单的解决方案,但我想避免这种情况。

我当时正在考虑为每个页面使用@ViewChildren,但这似乎不是我的最佳解决方案。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

只需将模态包装在容器中,然后按照documentation向其添加cdkDragRootElement。从component.ts打开对话框时,还必须将此类作为选项添加。

<ng-template #content let-modal>
    <div class="you-custom-dialog-class"
      cdkDrag
      cdkDragRootElement=".your-custom-dialog-class">

      <div class="modal-header"></div>
      <div class="modal-body"></div>
      <div class="modal-footer"></div>
    </div>
</ng-template>

该组件的代码。ts

const options: NgbModalOptions = {
    windowClass: 'your-custom-dialog-class'
};
this.modalService.open(this.content, options);