简体   繁体   English

NgbModal.open没有在Angular 4中工作

[英]NgbModal.open not working in Angular 4

I want to display a message in a modal using angular 4.0.0 and ng-bootstrap 1.0.0-beta.4 but it does not display the modal. 我想使用角度4.0.0和ng-bootstrap 1.0.0-beta.4在模态中显示消息,但它不显示模态。

app-module.ts APP-module.ts

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

login.component.ts 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 examplemodal.html

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

Any help please? 有什么帮助吗?

Here is how I implemented in my application: 以下是我在我的应用程序中实现的方式:

app.module.ts app.module.ts

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

Component from where I am opening the modal. 从我打开模态的组件。 LoginComponent in your case : 你的案例中的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: 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: 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>

Here working Demo 在这里工作演示

PLEASE NOTE: You would need to install Bootstrap 4 for SCSS. 请注意:您需要为SCSS安装Bootstrap 4。 npm install bootstrap -D and include its reference in your style.scss like : npm install bootstrap -D并在style.scss中包含它的引用,如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM