错误TypeError:无法在ngx-bootstrap中使用angular 6读取null的属性'createText'

问题描述 投票:3回答:2

我想在角度6中使用@ViewChild从不同的组件打开ngx-bootstrap模式,但是我收到此错误。

错误TypeError:无法在ComponentLoader.push中读取null的属性'createText'../ node_modules / ngx-bootstrap / component-loader / component-loader.class.js.ComponentLoader._getContentRef

在Inspect Element中,此标记也可见

<bs-modal-backdrop class="modal-backdrop fade in"></bs-modal-backdrop>  
angular twitter-bootstrap typescript ngx-bootstrap
2个回答
0
投票

你从哪里尝试接触孩子什么时候?

尝试在AfterViewInit()生命周期中打开它,因为在尝试加入它时可能他不是init。


0
投票

您可以使用选择器在视图中调用模态上的其他组件并使用

  • @Input()在被调用组件内发送数据
  • @Output() EventEmitter从被调用组件返回tada
  • OnChanges绑定被调用组件中的更改

像下面的例子

这是第一个组件:

import { Component, TemplateRef } from '@angular/core';
import { BsModalService, BsModalRef } from 'ngx-bootstrap/modal';

@Component({
  selector: 'demo-modal-service-static',
  templateUrl: './service-template.html'
})
export class DemoModalServiceStaticComponent {
  modalRef: BsModalRef;
  constructor(private modalService: BsModalService) {}

  openModal(template: TemplateRef<any>) {
    this.modalRef = this.modalService.show(template);
  }

  // Use change happen after modal
  public callAfterAdd(modifiedProduct){
  // close this.modalRef
  }
}

这是在Modal中调用的另一个组件:

import { Component, TemplateRef , OnInit, OnChanges, Input, Output, EventEmitter, SimpleChanges} from '@angular/core';
import { BsModalService, BsModalRef } from 'ngx-bootstrap/modal';

@Component({
  selector: 'app-product',
  templateUrl: './app-product.html' // put what you want in  view
})
export class AppProductComponent implements OnInit, OnChanges {
  @Input() product : any;
  @Output() afterAdd = new EventEmitter<any>();
  public id: number;
  constructor(private modalService: BsModalService) {}

  ngOnInit() {
  }

  public ngOnChanges (changes: SimpleChanges) {
    console.log(this.product);// see product here
    this.id = changes.product.currentValue.id;
    if(this.id){
        // see here if some change occur in the first component
    }
  }
  /**
  *This function returns the modified product in DemoModalServiceStaticComponent => callAfterAdd
  */
  someOtherFunction(){
  //change something in product 
    //then send the modified data to the 
    this.afterAdd.emit (this.product);
  }
}

这是第一个组件的视图

<button type="button" class="btn btn-primary" (click)="openModal(template)">Create template modal</button>

<ng-template #template>
  <div class="modal-header">
    <h4 class="modal-title pull-left">Modal</h4>
    <button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    <app-product [product]=dataTopass (afterAdd)="callAfterAdd(modifiedProduct)"></app-product>
  </div>
</ng-template>
© www.soinside.com 2019 - 2024. All rights reserved.