Angular `ng-content` is not working as expected with primeNg tables









up vote
6
down vote

favorite
1












I want to create a reusable table component which uses primeNg to render ui. I created a table.component.html and .ts for that. Now i want to render content for the table which will be table headers (th) and table body (body).



To do that, i am writing th and tbody implementation as a content of table and trying to render it in table.component.html using <ng-content></ng-content>. But the table is not displaying.



I tried adding the th and tbody directly in table.component.html and it displays the table. shouldn't ng-content do the same thing because the content has the same html?



Here is the link to snippet with example. Check table.component.html under shared dir. And app.component.html for initial start. comment the ng-content line and uncomment remaining lines in table.component.html and you should see the table.
https://stackblitz.com/edit/angular-playground-kwudxn?file=app%2Fshared%2Ftable%2Ftable.component.html










share|improve this question





















  • Hi - You source doesn't seems to be the working version - check you please check it
    – Rahul
    Nov 9 at 6:27














up vote
6
down vote

favorite
1












I want to create a reusable table component which uses primeNg to render ui. I created a table.component.html and .ts for that. Now i want to render content for the table which will be table headers (th) and table body (body).



To do that, i am writing th and tbody implementation as a content of table and trying to render it in table.component.html using <ng-content></ng-content>. But the table is not displaying.



I tried adding the th and tbody directly in table.component.html and it displays the table. shouldn't ng-content do the same thing because the content has the same html?



Here is the link to snippet with example. Check table.component.html under shared dir. And app.component.html for initial start. comment the ng-content line and uncomment remaining lines in table.component.html and you should see the table.
https://stackblitz.com/edit/angular-playground-kwudxn?file=app%2Fshared%2Ftable%2Ftable.component.html










share|improve this question





















  • Hi - You source doesn't seems to be the working version - check you please check it
    – Rahul
    Nov 9 at 6:27












up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





I want to create a reusable table component which uses primeNg to render ui. I created a table.component.html and .ts for that. Now i want to render content for the table which will be table headers (th) and table body (body).



To do that, i am writing th and tbody implementation as a content of table and trying to render it in table.component.html using <ng-content></ng-content>. But the table is not displaying.



I tried adding the th and tbody directly in table.component.html and it displays the table. shouldn't ng-content do the same thing because the content has the same html?



Here is the link to snippet with example. Check table.component.html under shared dir. And app.component.html for initial start. comment the ng-content line and uncomment remaining lines in table.component.html and you should see the table.
https://stackblitz.com/edit/angular-playground-kwudxn?file=app%2Fshared%2Ftable%2Ftable.component.html










share|improve this question













I want to create a reusable table component which uses primeNg to render ui. I created a table.component.html and .ts for that. Now i want to render content for the table which will be table headers (th) and table body (body).



To do that, i am writing th and tbody implementation as a content of table and trying to render it in table.component.html using <ng-content></ng-content>. But the table is not displaying.



I tried adding the th and tbody directly in table.component.html and it displays the table. shouldn't ng-content do the same thing because the content has the same html?



Here is the link to snippet with example. Check table.component.html under shared dir. And app.component.html for initial start. comment the ng-content line and uncomment remaining lines in table.component.html and you should see the table.
https://stackblitz.com/edit/angular-playground-kwudxn?file=app%2Fshared%2Ftable%2Ftable.component.html







angular primeng primeng-turbotable






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 5:57









Mouni R

354




354











  • Hi - You source doesn't seems to be the working version - check you please check it
    – Rahul
    Nov 9 at 6:27
















  • Hi - You source doesn't seems to be the working version - check you please check it
    – Rahul
    Nov 9 at 6:27















Hi - You source doesn't seems to be the working version - check you please check it
– Rahul
Nov 9 at 6:27




Hi - You source doesn't seems to be the working version - check you please check it
– Rahul
Nov 9 at 6:27












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










Problem with your application is that ng-template doesn't render anything so ng-content doesn't render anything either. I don't see currently any added value in your TableComponent as it currently only resends the templates, but that's maybe because it is just a demo and it will have some added value in your case.



You need to change your TableComponent to pick up the PrimeTemplates from the content and resend them to p-table:



export class TableComponent implements AfterContentInit 

@Input()
public data: any;
@ContentChildren(PrimeTemplate)
templates: QueryList<any>;
headerTemplate: TemplateRef<any>;
bodyTemplate: TemplateRef<any>;

constructor()

gePrimeTemplateByType(type: string): PrimeTemplate
return this.templates.find(template =>
return template.getType() === type;
);


ngAfterContentInit()
this.headerTemplate = this.gePrimeTemplateByType('header').template;
this.bodyTemplate = this.gePrimeTemplateByType('body').template;




And in your template:



<p-table [value]="data">
<ng-template pTemplate="header">
<ng-container *ngTemplateOutlet="headerTemplate">
</ng-container>
</ng-template>
<ng-template pTemplate="body" let-data>
<ng-container *ngTemplateOutlet="bodyTemplate; context:$implicit: data">
</ng-container>
</ng-template>
</p-table>


Here is complete forked stackblitz demo.



Of course there are other ways to implement this, e.g. your TableComponent could have just 2 @Inputs and just resend them to p-table instead of using PrimeTemplates.






share|improve this answer




















  • This is was exactly I was trying aside before posting an answer, nice way :) +1
    – Pankaj Parkar
    Nov 9 at 7:37










  • But I have to admit that, I was trying to load template first and then lazily going to instantiate p-table imperatively from Component.
    – Pankaj Parkar
    Nov 9 at 7:47

















up vote
0
down vote













ng-content is basically used for content projection. Try using <ng-template> instead of <ng-content>



Link:- https://angular-2-training-book.rangle.io/handout/components/projection.html



app/child/child.component.ts



import Component from '@angular/core';

@Component(
selector: 'child',
template: `
<div style="border: 1px solid blue; padding: 1rem;">
<h4>Child Component</h4>
<ng-content></ng-content>
</div>
`
)
export class ChildComponent



app/app.component.html



 <child>
<p>My <i>projected</i> content.</p>
</child>





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220566%2fangular-ng-content-is-not-working-as-expected-with-primeng-tables%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    Problem with your application is that ng-template doesn't render anything so ng-content doesn't render anything either. I don't see currently any added value in your TableComponent as it currently only resends the templates, but that's maybe because it is just a demo and it will have some added value in your case.



    You need to change your TableComponent to pick up the PrimeTemplates from the content and resend them to p-table:



    export class TableComponent implements AfterContentInit 

    @Input()
    public data: any;
    @ContentChildren(PrimeTemplate)
    templates: QueryList<any>;
    headerTemplate: TemplateRef<any>;
    bodyTemplate: TemplateRef<any>;

    constructor()

    gePrimeTemplateByType(type: string): PrimeTemplate
    return this.templates.find(template =>
    return template.getType() === type;
    );


    ngAfterContentInit()
    this.headerTemplate = this.gePrimeTemplateByType('header').template;
    this.bodyTemplate = this.gePrimeTemplateByType('body').template;




    And in your template:



    <p-table [value]="data">
    <ng-template pTemplate="header">
    <ng-container *ngTemplateOutlet="headerTemplate">
    </ng-container>
    </ng-template>
    <ng-template pTemplate="body" let-data>
    <ng-container *ngTemplateOutlet="bodyTemplate; context:$implicit: data">
    </ng-container>
    </ng-template>
    </p-table>


    Here is complete forked stackblitz demo.



    Of course there are other ways to implement this, e.g. your TableComponent could have just 2 @Inputs and just resend them to p-table instead of using PrimeTemplates.






    share|improve this answer




















    • This is was exactly I was trying aside before posting an answer, nice way :) +1
      – Pankaj Parkar
      Nov 9 at 7:37










    • But I have to admit that, I was trying to load template first and then lazily going to instantiate p-table imperatively from Component.
      – Pankaj Parkar
      Nov 9 at 7:47














    up vote
    3
    down vote



    accepted










    Problem with your application is that ng-template doesn't render anything so ng-content doesn't render anything either. I don't see currently any added value in your TableComponent as it currently only resends the templates, but that's maybe because it is just a demo and it will have some added value in your case.



    You need to change your TableComponent to pick up the PrimeTemplates from the content and resend them to p-table:



    export class TableComponent implements AfterContentInit 

    @Input()
    public data: any;
    @ContentChildren(PrimeTemplate)
    templates: QueryList<any>;
    headerTemplate: TemplateRef<any>;
    bodyTemplate: TemplateRef<any>;

    constructor()

    gePrimeTemplateByType(type: string): PrimeTemplate
    return this.templates.find(template =>
    return template.getType() === type;
    );


    ngAfterContentInit()
    this.headerTemplate = this.gePrimeTemplateByType('header').template;
    this.bodyTemplate = this.gePrimeTemplateByType('body').template;




    And in your template:



    <p-table [value]="data">
    <ng-template pTemplate="header">
    <ng-container *ngTemplateOutlet="headerTemplate">
    </ng-container>
    </ng-template>
    <ng-template pTemplate="body" let-data>
    <ng-container *ngTemplateOutlet="bodyTemplate; context:$implicit: data">
    </ng-container>
    </ng-template>
    </p-table>


    Here is complete forked stackblitz demo.



    Of course there are other ways to implement this, e.g. your TableComponent could have just 2 @Inputs and just resend them to p-table instead of using PrimeTemplates.






    share|improve this answer




















    • This is was exactly I was trying aside before posting an answer, nice way :) +1
      – Pankaj Parkar
      Nov 9 at 7:37










    • But I have to admit that, I was trying to load template first and then lazily going to instantiate p-table imperatively from Component.
      – Pankaj Parkar
      Nov 9 at 7:47












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    Problem with your application is that ng-template doesn't render anything so ng-content doesn't render anything either. I don't see currently any added value in your TableComponent as it currently only resends the templates, but that's maybe because it is just a demo and it will have some added value in your case.



    You need to change your TableComponent to pick up the PrimeTemplates from the content and resend them to p-table:



    export class TableComponent implements AfterContentInit 

    @Input()
    public data: any;
    @ContentChildren(PrimeTemplate)
    templates: QueryList<any>;
    headerTemplate: TemplateRef<any>;
    bodyTemplate: TemplateRef<any>;

    constructor()

    gePrimeTemplateByType(type: string): PrimeTemplate
    return this.templates.find(template =>
    return template.getType() === type;
    );


    ngAfterContentInit()
    this.headerTemplate = this.gePrimeTemplateByType('header').template;
    this.bodyTemplate = this.gePrimeTemplateByType('body').template;




    And in your template:



    <p-table [value]="data">
    <ng-template pTemplate="header">
    <ng-container *ngTemplateOutlet="headerTemplate">
    </ng-container>
    </ng-template>
    <ng-template pTemplate="body" let-data>
    <ng-container *ngTemplateOutlet="bodyTemplate; context:$implicit: data">
    </ng-container>
    </ng-template>
    </p-table>


    Here is complete forked stackblitz demo.



    Of course there are other ways to implement this, e.g. your TableComponent could have just 2 @Inputs and just resend them to p-table instead of using PrimeTemplates.






    share|improve this answer












    Problem with your application is that ng-template doesn't render anything so ng-content doesn't render anything either. I don't see currently any added value in your TableComponent as it currently only resends the templates, but that's maybe because it is just a demo and it will have some added value in your case.



    You need to change your TableComponent to pick up the PrimeTemplates from the content and resend them to p-table:



    export class TableComponent implements AfterContentInit 

    @Input()
    public data: any;
    @ContentChildren(PrimeTemplate)
    templates: QueryList<any>;
    headerTemplate: TemplateRef<any>;
    bodyTemplate: TemplateRef<any>;

    constructor()

    gePrimeTemplateByType(type: string): PrimeTemplate
    return this.templates.find(template =>
    return template.getType() === type;
    );


    ngAfterContentInit()
    this.headerTemplate = this.gePrimeTemplateByType('header').template;
    this.bodyTemplate = this.gePrimeTemplateByType('body').template;




    And in your template:



    <p-table [value]="data">
    <ng-template pTemplate="header">
    <ng-container *ngTemplateOutlet="headerTemplate">
    </ng-container>
    </ng-template>
    <ng-template pTemplate="body" let-data>
    <ng-container *ngTemplateOutlet="bodyTemplate; context:$implicit: data">
    </ng-container>
    </ng-template>
    </p-table>


    Here is complete forked stackblitz demo.



    Of course there are other ways to implement this, e.g. your TableComponent could have just 2 @Inputs and just resend them to p-table instead of using PrimeTemplates.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 9 at 6:54









    Ludevik

    4,25212143




    4,25212143











    • This is was exactly I was trying aside before posting an answer, nice way :) +1
      – Pankaj Parkar
      Nov 9 at 7:37










    • But I have to admit that, I was trying to load template first and then lazily going to instantiate p-table imperatively from Component.
      – Pankaj Parkar
      Nov 9 at 7:47
















    • This is was exactly I was trying aside before posting an answer, nice way :) +1
      – Pankaj Parkar
      Nov 9 at 7:37










    • But I have to admit that, I was trying to load template first and then lazily going to instantiate p-table imperatively from Component.
      – Pankaj Parkar
      Nov 9 at 7:47















    This is was exactly I was trying aside before posting an answer, nice way :) +1
    – Pankaj Parkar
    Nov 9 at 7:37




    This is was exactly I was trying aside before posting an answer, nice way :) +1
    – Pankaj Parkar
    Nov 9 at 7:37












    But I have to admit that, I was trying to load template first and then lazily going to instantiate p-table imperatively from Component.
    – Pankaj Parkar
    Nov 9 at 7:47




    But I have to admit that, I was trying to load template first and then lazily going to instantiate p-table imperatively from Component.
    – Pankaj Parkar
    Nov 9 at 7:47












    up vote
    0
    down vote













    ng-content is basically used for content projection. Try using <ng-template> instead of <ng-content>



    Link:- https://angular-2-training-book.rangle.io/handout/components/projection.html



    app/child/child.component.ts



    import Component from '@angular/core';

    @Component(
    selector: 'child',
    template: `
    <div style="border: 1px solid blue; padding: 1rem;">
    <h4>Child Component</h4>
    <ng-content></ng-content>
    </div>
    `
    )
    export class ChildComponent



    app/app.component.html



     <child>
    <p>My <i>projected</i> content.</p>
    </child>





    share|improve this answer


























      up vote
      0
      down vote













      ng-content is basically used for content projection. Try using <ng-template> instead of <ng-content>



      Link:- https://angular-2-training-book.rangle.io/handout/components/projection.html



      app/child/child.component.ts



      import Component from '@angular/core';

      @Component(
      selector: 'child',
      template: `
      <div style="border: 1px solid blue; padding: 1rem;">
      <h4>Child Component</h4>
      <ng-content></ng-content>
      </div>
      `
      )
      export class ChildComponent



      app/app.component.html



       <child>
      <p>My <i>projected</i> content.</p>
      </child>





      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        ng-content is basically used for content projection. Try using <ng-template> instead of <ng-content>



        Link:- https://angular-2-training-book.rangle.io/handout/components/projection.html



        app/child/child.component.ts



        import Component from '@angular/core';

        @Component(
        selector: 'child',
        template: `
        <div style="border: 1px solid blue; padding: 1rem;">
        <h4>Child Component</h4>
        <ng-content></ng-content>
        </div>
        `
        )
        export class ChildComponent



        app/app.component.html



         <child>
        <p>My <i>projected</i> content.</p>
        </child>





        share|improve this answer














        ng-content is basically used for content projection. Try using <ng-template> instead of <ng-content>



        Link:- https://angular-2-training-book.rangle.io/handout/components/projection.html



        app/child/child.component.ts



        import Component from '@angular/core';

        @Component(
        selector: 'child',
        template: `
        <div style="border: 1px solid blue; padding: 1rem;">
        <h4>Child Component</h4>
        <ng-content></ng-content>
        </div>
        `
        )
        export class ChildComponent



        app/app.component.html



         <child>
        <p>My <i>projected</i> content.</p>
        </child>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 0:29

























        answered Nov 9 at 6:47









        Mahi

        825319




        825319



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220566%2fangular-ng-content-is-not-working-as-expected-with-primeng-tables%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

            How do I collapse sections of code in Visual Studio Code for Windows?