【问题标题】:How to display simple modal with ng-bootstrap (Angular 2)如何使用 ng-bootstrap (Angular 2) 显示简单模式
【发布时间】:2017-04-23 04:24:43
【问题描述】:

我正在尝试按照以下页面展示一个带有 Ng2-bootstrap 的简单模态:https://ng-bootstrap.github.io/#/components/modal

但是我收到了以下错误:

Error in ./AppComponent class AppComponent - inline template:7:0 caused by: Missing modal container, add <template ngbModalContainer></template> to one of your application templates.

ORIGINAL EXCEPTION: Missing modal container, add <template ngbModalContainer></template> to one of your application templates.

我做错了什么?我有以下代码来尝试使这项工作:

Index.html:

<body> <app-root>Loading...</app-root> <template ngbModalContainer></template> </body>

app.module.ts:

import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; ... @NgModule({... imports: [ BrowserModule, FormsModule, HttpModule, NgbModule.forRoot() ...

app.component.ts:

import { NgbModal, NgbActiveModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
import { SasModalComponent } from './sas-modal/sas-modal.component';
...
constructor(..., private modalService: NgbModal){
}
...
open() {
  console.log("trying to open");
  const modalRef = this.modalService.open(SasModalComponent);
}

app.component.html

<button class="btn btn-lg btn-outline-primary" (click)="open(content)">Launch demo modal</button>

sas-modal.component.ts

import { NgbModal, NgbActiveModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
...
@Component({
  selector: 'sas-modal',
  templateUrl: './sas-modal.component.html',
  styleUrls: ['./sas-modal.component.css']
})
export class SasModalComponent {
  closeResult: string;

  constructor(private modalService: NgbModal) {}

  open(content) {
    this.modalService.open(content);
  }
}

sas-modal.component.html:

<template #content let-c="close" let-d="dismiss">
  <div class="modal-header">
    <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
      <span aria-hidden="true">&times;</span>
    </button>
    <h4 class="modal-title">Modal title</h4>
  </div>
  <div class="modal-body">
    <p>One fine body&hellip;</p>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-secondary" (click)="c('Close click')">Close</button>
  </div>
</template>

【问题讨论】:

  • 在@NgModule() 的appModule 文件中添加entryComponents: [SasModalComponent]
  • 你在 sas-modal.component.ts 中的 constructor() 应该是 :- constructor(public activeModal: NgbActiveModal){}。不要在构造函数中传递modalService,也不需要在sas-modal.component.ts中声明open函数

标签: javascript twitter-bootstrap angular ng2-bootstrap


【解决方案1】:

sas-component.ts 应该是:-

import { NgbModal, NgbActiveModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
...
@Component({
  selector: 'sas-modal',
  templateUrl: './sas-modal.component.html',
  styleUrls: ['./sas-modal.component.css']
})
export class SasModalComponent {
  closeResult: string;

  constructor(private activeModal: NgbActiveModal) {}

} 

app.module.ts 应该在声明数组中包含 SasModuleComponent 和一个 包含 SasModuleComponent 的 entryComponent 字段:-

@NgModule({
  imports: [ BrowserModule,
   FormsModule , HttpModule, NgbModule.forRoot()],
  declarations: [ AppComponent, SasModalComponent],
  bootstrap: [ AppComponent ],
  entryComponents: [SasModalComponent]
})

您需要在模块文件的@NgModule() 中添加一个entryComponent 字段,因为您在this.modalService.open(SasModalComponent) 处动态加载SasModuleComponent。

【讨论】:

  • 谢谢,这有助于让我更接近答案,但不是完全的答案,你有几件事错了,明天会发布答案:)
  • @MikeDub 这有什么问题?这对我有用。
【解决方案2】:

saurav1405 的回答为我提供了足够有用的信息来解决这个问题,但他的解决方案并没有 100% 适合我。

出于这个原因,我赞成 saurav1405 的回复,但不能声称是答案,因为我必须做更多的工作才能让它为我工作。

主要区别在于有一个单独的控制组件和内容组件。

这就是我的工作方式:

>> app.module.ts

注意:saurav1405 建议将 ModalContentComponent 放入 declarations, (可能是错字在再次审查他的 cmets 之后)。

但您需要将实际的模态自定义模态组件放在这里(在本例中为 SasModalComponent 和 SasModalContent)。

并且内容组件(SasModalContent)需要是入口组件。

import { SasModalComponent, SasModalContent } from './sas-modal/sas-modal.component';
...
@NgModule({
  declarations: [
    AppComponent,
    SasModalComponent,
    SasModalContent
  ],
  entryComponents: [SasModalContent],
...

>> app.component.html:

ngbModalContainer 声明应该与父组件 html 模板中的组件声明一起,而不是在 index.html 中,我认为这是我在问题中发布错误的主要原因.

<template ngbModalContainer></template>
<sas-modal-component></sas-modal-component>

>> sas-modal.component.ts:

此文件结合了控制组件 [sas-modal-component](打开内容/模板)和内容组件 [sas-modal-content] (即对活动模态和模态模板的引用)。

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

@Component({
  selector: 'sas-modal-content',
  templateUrl: 'sas-modal-content.html'
})
export class SasModalContent {
  @Input() name;
  constructor(public activeModal: NgbActiveModal) {  }
}

@Component({
  selector: 'sas-modal-component',
  templateUrl: 'sas-modal.component.html'
})
export class SasModalComponent {
  constructor(private modalService: NgbModal, private compiler: Compiler) {  }

  open() {
    //this.compiler.clearCacheFor(SasModalContent); //Only use when the template is caching when you don't want it to.
    const modalRef = this.modalService.open(SasModalContent);
    modalRef.componentInstance.name = 'World';
  }
}

>> sas-modal.component.html: 包含打开模式的按钮/控件

<h2> Click the button to open the modal</h2>
<button class="btn btn-lg btn-outline-primary" (click)="open()">Launch demo modal</button>

>> sas-modal-content.html: 保存模态模板 - 可以是任何你想要的

<div class="modal-header">
    <button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
        <span aria-hidden="true">&times;</span>
      </button>
    <h4 class="modal-title">Hi there!</h4>
</div>
<div class="modal-body card-info">
    <h3 style="color: white;">So nice to meet you</h3>
    <h4 style="color: white;">This modal is great!</h4>
</div>
<div class="modal-footer ">
    <button type="button" class="btn btn-secondary" (click)="activeModal.close('Close click')">Close</button>
</div>

【讨论】:

  • 是的,声明中有错字。我已经在答案中进行了编辑。
【解决方案3】:

尝试在 app-root 模板中添加&lt;template ngbModalContainer&gt;&lt;/template&gt;

示例:

@Component({
  selector: 'app-root',
  template: '<template ngbModalContainer></template>'
})

【讨论】:

  • 示例:@Component({ selector: 'app-root', template: '&lt;template ngbModalContainer&gt;&lt;/template&gt;' })
  • 那不行,页面上什么也不显示
  • 第1步:sas-modal.component.html
  • 不要在 sas-modal.component.html 中使用模板。请参阅引导演示 link
  • 在你的@NgModule中添加entryComponents: [SasModalComponent],
猜你喜欢
  • 2017-09-20
  • 2018-08-04
  • 1970-01-01
  • 2019-03-15
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-22
相关资源
最近更新 更多