【问题标题】:ngx bootstrap - Nest a component within a templatengx bootstrap - 在模板中嵌套组件
【发布时间】:2018-09-05 07:16:37
【问题描述】:

我们的应用程序中有一个标准模式。

<ng-template [ngIf]="true" #editDataModal>
   <div class="modal-header">
     <h5 class="modal-title">Edit Modal</h5>
     <button type="button" class="close" aria-label="Close" (click)="onCancelClicked()">
       <span aria-hidden="true">&times;</span>
     </button>
   </div>
  <div class="modal-body" #editDataModalBody>CUSTOM COMPONENT GOES HERE</div>
 </ng-template>

我们希望能够传入一个自定义组件作为我们的 body。 ngx bootstrap 有没有办法做到这一点?

模态框似乎出现在主要内容之外,因此我们无法使用 ViewChild 找到它。

我们使用模态服务调用它。像这样:-

  constructor(
    private modalService: BsModalService
  ) { }

ngOnInit() {
   this.modalConfig = {
      ignoreBackdropClick: true
    };

   this.ModalRef = this.modalService.show(this.editModal, this.modalConfig);
}

【问题讨论】:

    标签: angular6 ngx-bootstrap ngx-bootstrap-modal


    【解决方案1】:

    模态组件可以获取并发布ViewContainerRef。例如,它可以使用 BehaviorSubject。父组件可以创建自定义组件,并在 viewContainerRef 发布时将其添加到 modal 中。我只是这样做而不仅仅是一个吸气剂,因为 ViewChild 在 afterViewInit 之前是无效的,所以你需要一种方法来处理它。

    // EditModalComponent
    export class EditModalComponent implements AfterViewInit {
      @ViewChild("editDataModalBody", {read: ViewContainerRef}) vc: ViewContainerRef;
    
      public bodyRefSubject: BehaviorSubject<ViewContainerRef> = new BehaviorSubject<ViewContainerRef>(null);
    
      constructor(
        public bsModalRef: BsModalRef,
        public vcRef: ViewContainerRef
      ) {}
    
      ngAfterViewInit() {
        this.bodyRefSubject.next(this.vc);
      }
    
      onCancelClicked() {
        console.log('cancel clicked');
        this.bsModalRef.hide()
      }
    }
    

    并且在父组件中:

    // Parent Component
    export class AppComponent {
      bsModalRef: BsModalRef;
      bodyContainerRef: ViewContainerRef;
      customComponentFactory: ComponentFactory<CustomComponent>;
      modalConfig: ModalOptions;
    
      constructor(
        private modalService: BsModalService,
        private resolver: ComponentFactoryResolver
      ) {
        this.customComponentFactory = resolver.resolveComponentFactory(CustomComponent);
      }
    
      openModalWithComponent() {
        this.modalConfig = {
          ignoreBackdropClick: true,
        }
        this.bsModalRef = this.modalService.show(EditModalComponent, this.modalConfig);
        this.bsModalRef.content.bodyRefSubject.subscribe((ref) => {
          this.bodyContainerRef = ref;
          if (this.bodyContainerRef) {
            this.bodyContainerRef.createComponent(this.customComponentFactory);
          }
        })
      }
    }
    

    另一种不使用 ViewChild 的方法是在 div 上放置一个指令而不是#editDataModalBody,该指令可以注入 ViewContainerRef 并使用服务或类似的方式发布它。

    【讨论】:

      猜你喜欢
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      • 2020-04-06
      • 1970-01-01
      相关资源
      最近更新 更多