Angular 2+ ng-bootstrap模态传递HTML

时间:2018-10-10 20:18:08

标签: angular angular-components ng-bootstrap angular-library

好的,我的模态设置如下。 请记住,我在组件库中工作的不仅仅是应用程序。

在我的组件库中...

我有我的模板

 <div class="modal-header">
     <h4 class="mt-3">
        {{header}}
     </h4>
     <button id="messageModalClose" type="button" class="close" aria- 
      label="Close" 
         (click)="closeModal()">
         <span aria-hidden="true">&times;</span>
     </button>
  </div>
  <div class="modal-body">
      {{message}}
  </div>

和组件

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

@Component({
  selector: 'ms-modal',
  templateUrl: './ms-modal.component.html',
  styleUrls: ['./ms-modal.component.scss'],
  //encapsulation: ViewEncapsulation.None,
})
export class MsModalComponent implements OnInit {
  @Input() header: string;
  @Input() message:string;

  private _modalOptions: NgbModalOptions = {
    backdrop: 'static',
    keyboard: false,
    size: 'lg',
    centered: true
  };


  constructor(public activeModal: NgbActiveModal) { }

  ngOnInit() {

  }
  closeModal() {
    this.activeModal.close();
  }
}

我将组件导入到我的应用模块中     从“ ms-components”导入{msModalModule};

我将'msModalModule'添加到@NgModule导入数组中,还添加了组件 由模块引用到entryComponents

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
     BrowserModule,
     AppRoutingModule,
     BrowserAnimationsModule,
     HttpClientModule,
     FormsModule,
     msModalModule    
  ],
bootstrap: [AppComponent],
entryComponents:[MsModalComponent]
})
export class AppModule {}

现在在我的app.component.ts中,我添加了一个函数来处理打开模态并传入输入值

OpenModal(header,message){

***NgbModalOptions is optional
const modalOptions: NgbModalOptions = {
  backdrop: 'static',
  keyboard: false,
  size: 'lg',
  centered: true,
};


 const modalRef = this.modalService.open(MsModalComponent,modalOptions)
 modalRef.componentInstance.header = header;
 modalRef.componentInstance.message = message;


 }

因此,我不仅希望传递字符串值,而且还希望在refModal中传递/显示HTML和控件以使其更有用...我听说过“包含”,但仅找到AngularJS示例...这个权利?还有其他想法吗?

2 个答案:

答案 0 :(得分:1)

如果您只是希望将身体部位作为html。您可以使用<ng-content></ng-content>

<div class="modal-header">
     <h4 class="mt-3">
        {{header}}
     </h4>
     <button id="messageModalClose" type="button" class="close" aria- 
      label="Close" 
         (click)="closeModal()">
         <span aria-hidden="true">&times;</span>
     </button>
  </div>
  <div class="modal-body">
      <ng-content></ng-content>
  </div>

模态成分的使用

<ms-modal><h1> This is modal content area </h1></ms-modal>

答案 1 :(得分:0)

您似乎无法将表单控件传递给ng-bootstrap模态。

但是,我确实更改了“模态主体”,以便可以传递文本或HTML。 举例来说,这只会解释html样式标签(粗体)...不会解释控制标签,例如或

使用“ innerHTML”,看起来像这样。

        <div class="modal-header">
            <h4 class="mt-3">{{header}}
            </h4>
            <button id="messageModalClose" type="button" class="close" aria-label="Close" (click)="closeModal()">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body" [innerHTML]=message>

        </div>