我试图找到一些关于如何在Angular 2.0中进行确认模式对话框的示例.我一直在为Angular 1.0使用Bootstrap dialog,但在web上找不到Angular 2.0的任何示例.我还判断了angular 2.0文档,但运气不佳.

有没有办法在ANGLING 2.0中使用Bootstrap对话框?

推荐答案

  • Angular 2及以上
  • bootstrap css(保留动画)
  • NO JQuery
  • NO bootstrap.js
  • 支持custom modal content(就像接受的答案一样)
  • 最近增加了对multiple modals on top of each other的支持.

`

@Component({
  selector: 'app-component',
  template: `
  <button type="button" (click)="modal.show()">test</button>
  <app-modal #modal>
    <div class="app-modal-header">
      header
    </div>
    <div class="app-modal-body">
      Whatever content you like, form fields, anything
    </div>
    <div class="app-modal-footer">
      <button type="button" class="btn btn-default" (click)="modal.hide()">Close</button>
      <button type="button" class="btn btn-primary">Save changes</button>
    </div>
  </app-modal>
  `
})
export class AppComponent {
}

@Component({
  selector: 'app-modal',
  template: `
  <div (click)="onContainerClicked($event)" class="modal fade" tabindex="-1" [ngClass]="{'in': visibleAnimate}"
       [ngStyle]="{'display': visible ? 'block' : 'none', 'opacity': visibleAnimate ? 1 : 0}">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <ng-content select=".app-modal-header"></ng-content>
        </div>
        <div class="modal-body">
          <ng-content select=".app-modal-body"></ng-content>
        </div>
        <div class="modal-footer">
          <ng-content select=".app-modal-footer"></ng-content>
        </div>
      </div>
    </div>
  </div>
  `
})
export class ModalComponent {

  public visible = false;
  public visibleAnimate = false;

  public show(): void {
    this.visible = true;
    setTimeout(() => this.visibleAnimate = true, 100);
  }

  public hide(): void {
    this.visibleAnimate = false;
    setTimeout(() => this.visible = false, 300);
  }

  public onContainerClicked(event: MouseEvent): void {
    if ((<HTMLElement>event.target).classList.contains('modal')) {
      this.hide();
    }
  }
}

To show the backdrop,您将需要类似这样的CSS:

.modal {
  background: rgba(0,0,0,0.6);
}

The example now allows for multiple modals at the same time.(参见onContainerClicked()法).

For Bootstrap 4 css users,您需要做1个小更改(因为CSS类名是从Bootstrap 3更新的).这一行: [ngClass]="{'in': visibleAnimate}"应更改为: [ngClass]="{'show': visibleAnimate}"

为了演示,这里有一个100

Angular相关问答推荐

Angular - Bootstrap 5 Carbon不工作

使Angular 效果只执行一次

PrimeNg选项卡视图选项卡

是否自动处理Angular /RxJS观测数据的取消订阅

类是一个独立的组件,不能在Angular中的`@NgModule.bootstrap`数组中使用

角路径S异步旋转变压器仍可观察到

如何在独立应用程序中全局禁用基于媒体查询的Angular 动画?

合并Cypress和Karma的代码覆盖率报告JSON文件不会产生正确的结果

Angular 不使用NgRx的简单CRUD应用程序

Angular6:RxJS switchMap 运算符未按预期工作

将 autoSpy 与 ng-mocks 一起使用时,如何重置 jasmine 间谍调用?

如何使用指令以Angular 传递默认值

Angular keypress 存储空间而不是第一个字母

如何在两个模块之间共享服务 - @NgModule 在Angular 而不是在组件之间?

Angular2 路由 VS ui-router-ng2 VS ngrx 路由

`[(ngModel)]` vs `[(value)]`

angular4的所见即所得编辑器

Lint 错误:实现生命周期挂钩接口

如何导航到同级路由?

如何作为模块的子模块路由到模块 - Angular 2 RC 5