1

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

从字面上看,只是将这个 w3schools 折叠示例复制到我的模态中。

这是我的模态代码:

<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>

我的基本模态组件:

@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 中折叠的行为,好像当你折叠<div>它时它会添加一些类和一些属性,而当它折叠回来时它也会改变它们。

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

有任何想法吗?

4

2 回答 2

3

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

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

于 2016-09-07T15:00:55.563 回答
1

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 示例

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

您将使用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 示例

于 2016-09-05T20:10:24.087 回答