javascript – Angular 2:NgbModal在视图中转换

假设我有这样的模态模板:
<div class="modal-header">
  <h3 [innerHtml]="header"></h3>
</div>

<div class="modal-body">
  <ng-content></ng-content>
</div>

<div class="modal-footer">
</div>

我从另一个组件调用这个模态,所以:

const modalRef = this.modalService.open(MobileDropdownModalComponent,{
      keyboard: false,backdrop: 'static'
    });

    modalRef.componentInstance.header = this.text;

如何使用绑定等将NgbModal html放入?进入ng-content

解决方法

您可以从open方法返回的NgbModalRef中获取对组件实例的引用,并在那里设置binging.

这是打开模态的方法

open() {
   const instance = this.modalService.open(MyComponent).componentInstance;
   instance.name = 'Julia';
 }

这里是将使用一个输入绑定在模态中显示的组件

export class MyComponent {
   @input() name: string;

   constructor() {
   }
 }

===

您也可以传递templateRef作为输入.假设父组件有

<ng-template #tpl>hi there</ng-template>


 export class AppComponent {
   @ViewChild('tpl') tpl: TemplateRef<any>;

  constructor(private modalService: NgbModal) {
  }

 open() {
    const instance = 
    this.modalService.open(MyComponent).componentInstance;
     instance.tpl = this.tpl;
  }
}

和MyComponent:

export class MyComponentComponent {
  @input() tpl;

  constructor(private viewContainerRef: ViewContainerRef) {
  }

  ngAfterViewInit() {
     this.viewContainerRef.createEmbeddedView(this.tpl);
  }
}

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...