【问题标题】:Collapse doesn't work in a Modal折叠在模态中不起作用
【发布时间】:2017-01-12 20:21:22
【问题描述】:

我正在尝试在 Modal 中使用最基本的折叠功能,但折叠不会触发

实际上只是将this w3schools collapse example 复制到我的模态中。

这是我的模态代码:

<template #content let-c="close" let-d="dismiss" ngbModalContainer>
  <div class="modal-header">
    <h4 class="modal-title">Collapse</h4>
  </div>
  <form>
    <div class="modal-body">
      <button type="button" class="btn btn-info" data-toggle="collapse" data-target="#demo">Simple collapsible</button>
      <div id="demo" class="collapse">
            This is the collapsible text!
      </div>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-secondary" (click)="c('Close click')">Close</button>
    </div>
  </form>
</template>

<button class="btn btn-success" (click)="open(content)">Open Modal</button>

我的 BasicModalComponent:

@Component({
  selector: 'basic-modal',
  templateUrl: './BasicModal.html'
})
export class BasicModalComponent {
  closeResult: string;

  constructor(private modalService: NgbModal) {}

  open(content) {
    this.modalService.open(content).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  private getDismissReason(reason: any): string {
    if (reason === ModalDismissReasons.ESC) {
      return 'by pressing ESC';
    } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
      return 'by clicking on a backdrop';
    } else {
      return `with: ${reason}`;
    }
  }
}

我的应用组件:

@Component({
    selector: 'my-app',
    template: '<basic-modal></basic-modal>'
})
export class AppComponent { }

我的应用模块:

@NgModule({
    imports: [NgbModule, FormsModule],
    declarations: [AppComponent, BasicModalComponent],
    bootstrap: [AppComponent],
})
export class AppModule {
}

我尝试调试 DOM 中的折叠行为,似乎当您折叠 &lt;div&gt; 时,它会添加一些类和一些属性,而当将其折叠回来时,它也会更改它们。

当我在 Modal 中调试它时,触发折叠按钮不会操纵 DOM,&lt;div&gt; 的类及其属性保持不变。

有什么想法吗?

【问题讨论】:

  • @yurzui 谢谢你的回复,能解释一下吗?
  • 引导折叠事件单击文档 take.ms/mAhbw 上的句柄,但 ModalWindow 组件通过 stopPropagation 中断冒泡
  • @yurzui 嗯,我明白了。那么如果我想在我的模态中添加一个可折叠的部分,我应该怎么做?
  • 在下面查看我的答案

标签: javascript angular modal-dialog collapse ng-bootstrap


【解决方案1】:

ng-bootstrap 强烈反对将 Angular 2 小部件与 Bootstrap 的基于 jQuery 的 javascript 混合使用。事实上,使用像 ng-bootstrap 这样的库的全部意义在于使用 Bootstrap 的 JS。

你应该做的是使用折叠指令:ngbCollapse

【讨论】:

  • 我明白了,有没有办法实现bootstrap与ngbCollapse组件合拢的动画?
  • 本机指令的动画目前正在进行中。该项目非常活跃,因此动画落地应该不会花很长时间。
【解决方案2】:

在你的情况下,你可以“猴子补丁”ModalWindow,如下所述:

open(content) {
    // get reference to NgbModalRef
    let modal: any = this.modalService.open(content);
    modal.result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });

    let cancelPropagation = false;

    // overriding methods of NgbModalWindow 
    Object.assign(modal._windowCmptRef.instance.constructor.prototype, {
        stopPropagation: () => {
          cancelPropagation = true;
        },
        backdropClick: function() {
          if(cancelPropagation) { 
            cancelPropagation = false;
            return;
          }
          if (this.backdrop === true) {
            this.dismiss(ModalDismissReasons.BACKDROP_CLICK);
          }  
        }
    });
  }

Plunker Example

但这是非常肮脏的方式,因为它使用的是私有财产。

您将使用 NgbCollapse 指令,它是 ng-bootstrap 包的一部分,例如:

<button type="button" class="btn btn-info" (click)="isCollapsed = !isCollapsed">
   Simple collapsible
</button>
<div [ngbCollapse]="isCollapsed">
   This is the collapsible text!
</div>

Plunker Example

【讨论】:

  • 所以我采纳了您的建议并使用了ngbCollapse 而不是引导程序崩溃,它确实有效。我现在只想知道,有没有办法给它与引导程序崩溃时的崩溃动画?
猜你喜欢
  • 2018-12-01
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多