首页 文章

从组件角度2获取ng-template

提问于
浏览
4

我怎么能得到角2的元素?如果我有这个在HTML中

<ng-template #content let-c="close" let-d="dismiss">
  <div class="modal-header">Header</div>
   <div class="modal-body">Body</div>
  <div class="modal-footer">footer</div>
</ng-template>

我使用它作为ngBmodal ng-bootstrap,如果我使用按钮打开内容,它的工作=按钮

(click)="open(content,data.id)"

那么我想在这种情况下从组件打开内容,从其他页面和开放内容重定向

ngOnInit() {
    this.activatedRoute.queryParams.subscribe((params: Params) => {
        let id = params['id'];
        if(id != undefined){
          this.open('content',id);         
        }
      });
  }
open(content, id) {
    this.dataModal = {};
    this.getDataModal(id);

    this.mr = this.modalService.open(content, { size: 'lg' });
  }

模态打开,但没有与HTML,我尝试afterviewinit得到#content它不工作谢谢,抱歉我的英语:v

1 回答

  • 0

    首先导入NgbModal和ModalDismissReadons并将modalservice添加到构造函数,请参阅:

    https://ng-bootstrap.github.io/#/components/modal/examples#options

    然后在你的打字稿文件中:

    1 - Import TemplateRef and ViewChild, example:

    import { TemplateRef, ViewChild } from '@angular/core';
    

    2 - Create the variable that binds the ngtemplate (add before constructor):

    @ViewChild('content')
    private content: TemplateRef<any>;
    

    3 - Open modal from typescript:

    this.modalService.open(this.content);
    

相关问题