简体   繁体   English

在带有模板的 Angular ng-bootstrap 模态中,关闭和关闭按钮不起作用

[英]In Angular ng-bootstrap modal with template, close and dismiss buttons not working

I am new to angular and I am learning by building.我是 angular 的新手,我正在通过构建来学习。

I have a requirement to open a modal programmatically with varying HTML Content.我需要以编程方式打开具有不同 HTML 内容的模式。

I have referred the first stackblitz example illustrated in this issue and created a ModalComponent (with selector called <app-modal> ), so that I can create modals anywhere in my app.我所提到的第一stackblitz 例如在此示出的问题,创造了ModalComponent(具有选择称为<app-modal> ),这样我可以在我的应用程序的任何位置创建模态。

Now, I use this component with varying html content in a <ng-template> in my page.现在,我在我的页面的<ng-template>中使用这个具有不同 html 内容的组件。

The modal opens fine on clicking the button in the main page, but the close and dismiss buttons in the modal do not work.单击主页面中的按钮时,模态打开正常,但模态中的关闭和关闭按钮不起作用。

I have tried to make a minimal example here : https://stackblitz.com/edit/angular-j1u4fo我试图在这里做一个最小的例子: https : //stackblitz.com/edit/angular-j1u4fo

Any help will be appreciated.任何帮助将不胜感激。 Thanks.谢谢。

Try this尝试这个

I have created Global configuration of modals with let-c="close" and let-d="dismiss"我用 let-c="close" 和 let-d="dismiss" 创建了模态的全局配置

app.component.html应用程序组件.html

<div class="container-fluid">
<p>Welcome to {{ name }}</p>
<button type="button" (click)="openModal()">Click to open Modal</button>

<!-- Example 1: Passing TemplateRef as custom component -->
  <ng-template #modal1 let-c="close" let-d="dismiss">
    <app-modal [title]="'Title'" [c]="c" [d]="d">
      <img src="https://angular.io/assets/images/logos/angular/angular.png" height="100px" width="100px">
    </app-modal>
  </ng-template>
</div>

modal.component.html modal.component.html

<div class="modal-header">
    <h4 class="modal-title">{{title}}</h4>
    <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
        <span aria-hidden="true">&times;</span>
    </button>
</div>
<div class="modal-body">
    <ng-content></ng-content>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-secondary" (click)="c('Close click')">Cancel</button>
</div>

modal.component.ts modal.component.ts

import { Component, Input, OnInit } from '@angular/core';

import {NgbModal, NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';


@Component({
  selector: 'app-modal',
  templateUrl: './modal.component.html',
  styleUrls: ['./modal.component.css']
})
export class ModalComponent implements OnInit {

  @Input() title = `Information`;
  @Input() c;
  @Input() d;

  constructor(
    public activeModal: NgbActiveModal
  ) {}

  ngOnInit() {
  }

}

Replace this.m1 with ModalComponent in your call to openModal .更换this.m1ModalComponent在您的来电openModal

According to the API documentation , NgbActiveModal only works when you pass in a component instead of the template.根据API 文档,NgbActiveModal 仅在您传入组件而不是模板时才起作用。

If you pass a component type as content, then instances of those components can be injected with an instance of the NgbActiveModal class.如果您将组件类型作为内容传递,则可以使用 NgbActiveModal 类的实例注入这些组件的实例。 You can then use NgbActiveModal methods to close / dismiss modals from "inside" of your component.然后,您可以使用 NgbActiveModal 方法从组件的“内部”关闭/关闭模态。

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

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