angular2-template – 从组件角度2获取ng-template

前端之家收集整理的这篇文章主要介绍了angular2-template – 从组件角度2获取ng-template前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我怎么能得到角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

解决方法

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

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

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

1 – 导入TemplateRef和ViewChild,例如:

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

2 – 创建绑定ngtemplate的变量(在构造函数之前添加):

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

3 – 打字稿中的打开模态:

this.modalService.open(this.content);

猜你在找的Angularjs相关文章