【问题标题】:How to pass component as Modal-body in Ngx-bootstrap modal in Angular 6如何在Angular 6中的Ngx-bootstrap模态中将组件作为模态体传递
【发布时间】:2019-01-04 09:35:33
【问题描述】:

我在 Angular 6 中将 ngx-bootstrap 用于模态,我创建了一个共享模态,我想在模态正文部分添加不同的 Html 模板。如何添加 HTML 模板或如何将动态组件传递给共享模式。

共享模态组件:

@Component({
selector: 'modal-content',
template: `
<div class="modal-header">
  <h4 class="modal-title">{{headerText}}</h4>
</div>
  <div *ngIf="showTemplate" class="modal-body">
   <templateselector></templateselector>
 </div>
<div class="modal-footer">
  <button  type="button" class="btn btn-primary" (click)="clickOk()"> 
  {{actionButtonText}}</button>
</div>
`
 })
export class ModalContentComponent{
 headerText: string; 
 actionButtonText: string;
 showTemplate: boolean;
 templateselector: any;  

 constructor(public bsModalRef: BsModalRef) { }

 clickOk() {
    this.bsModalRef.hide();
 }

调用组件:

@Component({
selector: 'modal-content',
template: `
   <div>
  <button type="button" class="btn" 
  (click)="openModalWithComponent()">modal</button>
   </div>`
  })

export class DynamicComponent{

constructor(public bsModalRef: BsModalRef) { }

openModalWithComponent(message: string, title: string) {
    const initialState = {
        headerText: title, 
        actionButtonText: "Continue",
        showTemplate: true,
        templateSelector : NewComponent //From here i need to pass different 
        //HTML which should get load in Modal body.
};
    this.bsModalRef = this.modalService.show(ModalContentComponent, { 
    initialState });

}

从调用组件我应该能够传递组件的名称,我不想对选择器进行硬编码,因为组件会发生变化,并且应该加载该组件以及模态页眉和页脚。

【问题讨论】:

  • @ColbyHunter 这个问题是不同的,因为我想通过调用组件将不同的组件传递给模态体。例如:我有 6 个组件,我想从一个主要组件显示为模态,因此我创建了一个共享 modalComponent,我将根据条件将 6 个组件作为模态体传递。谢谢
  • 这不是真的,答案就在那里,它只是没有解决您的具体问题。还有关于它的信息herefrom the official docs here

标签: angular ngx-bootstrap-modal


【解决方案1】:

代替

this.bsModalRef = this.modalService.show(ModalContentComponent, { initialState });

你可能想去掉括号,只使用对象作为第二个参数:

this.bsModalRef = this.modalService.show(ModalContentComponent, initialState);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    相关资源
    最近更新 更多