简体   繁体   English

ng-bootstrap模态页眉和页脚不显示?

[英]ng-bootstrap modal header and footer not displaying?

I am using Bootstrap 4 so I was able to launch modal window and rendered data from the component but I don't see header and footer that I have part of HTML I can not close the modal window. 我使用的是Bootstrap 4,因此能够启动模式窗口并从组件中渲染数据,但是我看不到我具有HTML的页眉和页脚,因此无法关闭模式窗口。 What is missing in below code? 以下代码缺少什么?

modal.component.ts modal.component.ts

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

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

@Component({
  selector: 'ngbd-modal-content',
  templateUrl: './modal.component.html',
})
export class NgbdModalContent {
  @Input() name;

  constructor(public activeModal: NgbActiveModal) {}
}

modal.component.html modal.component.html

<ng-template #theModal let-c="close" let-d="dismiss">
  <div class="modal-header">
    <h4 *ngIf="type == 0" class="modal-title">Header</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">
  </div>
  <div class="modal-footer">
    <button type="button" id="cancel-edit-btn" class="btn btn-primary" (click)="c('Close click')">Cancel</button>
  </div>
</ng-template>

home.component.ts home.component.ts

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

export class HomeComponent implements OnChanges{

constructor(private detailService: DetailService,private ngbModal: NgbModal) {};

 onClick(evt){
     const modalRef = this.ngbModal.open(HomeComponent,{ size: 'lg', backdrop: 'static' });
   }

}

I know this is an old question but here is an answer 我知道这是一个老问题,但这是一个答案

I had the same issue and if you look close at the example provided they make use of the template reference variable ( #content ). 我遇到了同样的问题,如果您仔细查看示例,他们将使用模板引用变量(#content)。 To get it working with minor alteration you can import the additional elements required; 为了使其能够进行较小的改动,您可以导入所需的其他元素。

ViewChild and ElementRef ViewChild和ElementRef

import { Component, OnInit, ViewEncapsulation, ElementRef, ViewChild } from '@angular/core';

then under the closeResult variable add a reference to the templated variable 然后在closeResult变量下添加对模板变量的引用

closeResult: string; @ViewChild('content') content: ElementRef;

Then the header and footer will be shown as the content is rendered correctly with all the mark , otherwise content would just be the body and text you pass. 然后,在正确标记所有内容后,将显示页眉和页脚,否则内容将只是您传递的正文和文本。

Cheers 干杯

Neil 尼尔

Using ngbModal you can, open a modal that belongs to your component (in this case your button and your are in the same html, (it's the first example) 您可以使用ngbModal打开属于您的组件的模式(在这种情况下,您的按钮和您位于同一个html中,这是第一个示例)

<ng-template #content let-c="close" let-d="dismiss">
...
</ng-template>

<button (click)="open(content)">Launch demo modal</button>

or open a component that was modal. 或打开模态组件。 In this case, the button is in one component, and the component modal is another The modal component is like . 在这种情况下,按钮在一个组件中,而组件模式是另一个组件。模式组件类似于。 See that there aren not "ng-template". 看到没有“ ng-template”。 And in the .ts it's necesary import NgbModal, NgbActiveModal to get function to close button 在.ts文件中,需要导入NgbModal,NgbActiveModal以获取关闭按钮的功能

<div class="modal-header">
      <h4 class="modal-title">Hi there!</h4>
      <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">
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-outline-dark" (click)="activeModal.close('Close click')">Close</button>
    </div>

The component that open the modal must import NgbModal and put in the constructor to open the modal 打开模式的组件必须导入NgbModal并放入构造函数中才能打开模式

There are a few classes and components that make up a Bootstrap 4 modal. 有一些类和组件构成了Bootstrap 4模式。 You can't just leave out some of those classes and components and then hope to still get a working modal. 您不能只忽略其中的一些类和组件,然后希望仍然可以使用一种工作模式。

Use the following code snippet as a template. 使用以下代码片段作为模板。 Your code needs to produce that HTML if you want a working modal. 如果需要工作模式,您的代码需要产生该HTML。 Don't leave out the necessary parts (divs & classes). 不要遗漏必要的部分(div和类)。

Click the "run code snippet" button below to see it working: 点击下面的“运行代码段”按钮,即可查看其运行情况:

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> <!-- Button trigger modal --> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"> Launch demo modal </button> <!-- Modal --> <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> ... </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> 

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

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