Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 ng-template not rendering in dom inside component

I have a component like so

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");
        });
    }

}

the html is, notice the popover component at the very top

<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>

The open component is just a component to display html

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

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

    constructor(
    ) {
    }

}

and the html for the popover component

<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>

My issue is when I have ng-template wrapped around test, the h1 test element doesn't render in the dom. More explicitly, ng-template doesn't render which is what I need to pass the #popover to the open function to open the popover.

When I remove the ng-template, I can see h1 test. I need the ng-template to render in the dom and I'm putting i in a component because there's a lot of html in the popover component.

Any idea why this is behaving this way?

like image 374
Garuuk Avatar asked Dec 24 '22 18:12

Garuuk


1 Answers

use <ng-container> instead

https://angular.io/guide/structural-directives#!#ng-container

like image 173
Thyagarajan C Avatar answered Dec 28 '22 09:12

Thyagarajan C