Angular 2 ng-template 不在组件内部的 dom 中呈现

标签 angular ng-template

我有这样一个组件

import {Component, OnInit} from '@angular/core';
import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';

@Component({
    selector: 'details',
    templateUrl: 'details.component.html',
    styleUrls: ["./details.component.scss"],
})
export class DetailsComponent {

    constructor(
        private modalService: NgbModal
    ) {
    }

    open(content) {
        this.modalService.open(content).result.then((result) => {
            console.log("closed");
        }, (reason) => {
            console.log("dismissed");
        });
    }

}

html 是,注意最顶部的 popover 组件

<popover></popover>
<div class="row">
    <div class="col">
        <table class="table">
            <thead>
                <th>Action</th>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <button (click)="open(popover)"> open </button>
                    </td>
                </tr>

            </tbody>
        </table>
    </div>
</div>

open组件只是一个显示html的组件

import {Component, OnInit} from '@angular/core';

@Component({
    selector: 'popover',
    templateUrl: 'popover.component.html',
    styleUrls: ["./popover.component.scss"],
})
export class PopOverComponent {

    constructor(
    ) {
    }

}

和弹出组件的 html

<ng-template #popover let-c="close" let-d="dismiss">
    <div class="modal-header">
    <h4 class="modal-title">Modal title</h4>
        <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    <div class="modal-body">
      <p>One fine body&hellip;</p>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" (click)="c('Close click')">Close</button>
    </div>
</ng-template>

我的问题是,当我将 ng-template 包裹在测试中时,h1 测试元素不会在 dom 中呈现。更明确地说,ng-template 不呈现,这是我需要将 #popover 传递给 open 函数以打开 popover 的内容。

当我删除 ng-template 时,我可以看到 h1 测试。我需要 ng-template 在 dom 中呈现,我将 i 放在一个组件中,因为 popover 组件中有很多 html。

知道为什么会这样吗?

最佳答案

关于Angular 2 ng-template 不在组件内部的 dom 中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43530085/

相关文章:

javascript - Ionic 2 可重用页面组件

angular - Office.js : Increasingly poorer performance writing a large amount of rows to Excel

javascript - Angular:向组件传递一个函数,该函数一旦在目标组件中执行就应该调用本地(到目标组件)方法

Angular 4 和 ng-模板

带有新 Observable 和 ng-template 的 Angular 4 异步管道不起作用

javascript - 显示垫子离场 Angular 9 的组件误差

Angular 2 : possible to combine app shell together with universal rendering

angular - 如何在自定义组件中使用 DxTemplate

javascript - Angular 2 编写像 ngFor、iterableDiffer 这样的结构指令可能会导致错误

angular - 如何将 ng-template innerHTML 获取到组件