【问题标题】:NgbModal.open not working in Angular 4NgbModal.open 在 Angular 4 中不起作用
【发布时间】:2018-08-21 00:34:54
【问题描述】:

我想使用 Angular 4.0.0 和 ng-bootstrap 1.0.0-beta.4 但它不显示模式。

应用模块.ts

@NgModule({
// ...
declarations: [
  LoginComponent
],
imports: [
// ...
  NgbModule.forRoot()
],
entryComponents: [LoginComponent],
})
export class AppModule { }

login.component.ts

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent {
   @ViewChild('examplemodal')
   private modalRef: TemplateRef<any>;

   constructor(private modalService: NgbModal) { }

   //.... some event that fires onModalRequest() 

   onModalRequest(): void {
    const modalRef = this.modalService.open(this.modalRef); //Not working

    modalRef.result.then((result) => {
      console.log(result);
      console.log('closed');
    }).catch( (result) => {
      console.log(result);
      console.log('cancelling');
    });
  }
}

examplemodal.html

<ng-template #examplemodal>
  <div class="modal">
    stuff here...
  </div>
</ng-template>

有什么帮助吗?

【问题讨论】:

  • 根据 API:内容可以作为 TemplateRef 或组件类型提供。你提供字符串.. 来源:ng-bootstrap.github.io/#/components/modal/api
  • 你是对的!请检查我的编辑,我已经添加了一个 TemplateRef 但仍然无法正常工作。 @echonax
  • 这个有错误吗?
  • @RafaelReyes - 您是否在项目中包含了 Bootstrap 4 CSS?我认为您使用的 ng-bootstrap 版本需要旧版本的 Bootstrap(类似于 4.0.0-beta.3)。
  • 不,我使用的是 Bootstrap 3.3.7 @ConnorsFan

标签: angular modal-dialog ng-bootstrap


【解决方案1】:

这是我在应用程序中的实现方式:

app.module.ts

@NgModule({
// ...
declarations: [
  LoginComponent,
  ModalComponent
],
imports: [
// ...
  NgbModule.forRoot()
],
entryComponents: [ModalComponent], //Only Modal component included here
bootstrap: [AppComponent]
})
export class AppModule { }

我打开模态的组件。 LoginComponent 在您的情况下:

imports ....
import { ModalComponent} from '../Modal/Modal.component'; //My actual Modal component which includes HTML for modal

    @Component({
      selector: 'app-login',
      templateUrl: './login.component.html',
      styleUrls: ['./login.component.css']
    })
    export class LoginComponent {
       @ViewChild('examplemodal')
       private modalRef: TemplateRef<any>;

       constructor(private modalService: NgbModal) { }

       //.... some event that fires onModalRequest() 

       onModalRequest(): void {
        const modalRef = this.modalService.open(ModalComponent); //Added Modal Component here

         modalRef.componentInstance.src = link;//anything u wish to pass to modal component @Input 

 modalRef.result.then((result) => {
      console.log(result);
      console.log('closed');
    }).catch( (result) => {
      console.log(result);
      console.log('cancelling');
    });

      }
    }

Modal.component.ts:

import { Component, OnInit, Input } from '@angular/core';
import { NgbModal, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
@Component({
  selector: 'app-modal',
  templateUrl: './modal.component.html',
  styleUrls: ['./modal.component.scss']
})
export class ModalComponent implements OnInit {
  @Input() src;
  constructor(public activeModal: NgbActiveModal) { }

  ngOnInit() {
  }

}

Modal.component.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>
</div>
<div class="modal-body">
          STUFF HERE
</div>

在这里工作Demo

请注意:您需要为 SCSS 安装 Bootstrap 4。 npm install bootstrap -D 并将其引用包含在您的 style.scss 中,例如:

@import "node_modules/bootstrap/scss/bootstrap";

【讨论】:

    猜你喜欢
    • 2018-10-19
    • 2018-02-26
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    相关资源
    最近更新 更多