折叠在模态中不起作用

时间:2016-09-05 12:49:16

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

我正在尝试使用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}`;
    }
  }
}

我的AppComponent:

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

我的AppModule:

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

我尝试在DOM中调试崩溃的行为,看起来当你折叠<div>时,它会添加一些类和几个属性,当它折叠回来时它也会改变它们。

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

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

在你的情况下,你可以&#34;猴子补丁&#34; 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