How to dynamically add innerHTML with angular 2 components












8















I'm creating docs for a component library. I want 1 string of html that generates both the component on the page and the docs for it.



What I want:



enter image description here



What I have:



enter image description here



When I inspect the HTML, my-button tags are not present. They are being stripped out when I use innerHTML.



My component code:



private flatButtons = `<div class="button-wrapper">
<my-button [type]="'default'" [raised]="false">Default</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'primary'" [raised]="false">Primary</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'success'" [raised]="false">Success</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'info'" [raised]="false">Info</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'warning'" [raised]="false">Warning</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'danger'" [raised]="false">Danger</my-button>
</div>`

constructor() {}

getCode() {
return html_beautify(this.flatButtons, this.options)
}


My HTML template:



<div class="row">
<div class="col-sm-6 col-xs-12">
<mi-card title="Flat Buttons" baCardClass="with-scroll button-panel">
<div id="flatButtons" [innerHTML]="getCode()">
</div>
</mi-card>
</div>
<div class="col-sm-6 col-xs-12">
<pre>{{getCode()}}</pre>
</div>











share|improve this question























  • What do you expect to happen?

    – Günter Zöchbauer
    Nov 7 '16 at 20:57











  • I expect the picture after "What I want:"

    – collinglass
    Nov 8 '16 at 14:52











  • Ok, so you expect the added HTML to become actual Angular components. That doesn't work this way. See my answer.

    – Günter Zöchbauer
    Nov 8 '16 at 14:53











  • this worked for me: stackoverflow.com/a/43862386/1048800

    – jgatjens
    May 9 '17 at 6:16











  • Maybe this can help: github.com/apoterenko/angular2-dynamic-component

    – Eusthace
    May 19 '17 at 0:59
















8















I'm creating docs for a component library. I want 1 string of html that generates both the component on the page and the docs for it.



What I want:



enter image description here



What I have:



enter image description here



When I inspect the HTML, my-button tags are not present. They are being stripped out when I use innerHTML.



My component code:



private flatButtons = `<div class="button-wrapper">
<my-button [type]="'default'" [raised]="false">Default</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'primary'" [raised]="false">Primary</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'success'" [raised]="false">Success</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'info'" [raised]="false">Info</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'warning'" [raised]="false">Warning</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'danger'" [raised]="false">Danger</my-button>
</div>`

constructor() {}

getCode() {
return html_beautify(this.flatButtons, this.options)
}


My HTML template:



<div class="row">
<div class="col-sm-6 col-xs-12">
<mi-card title="Flat Buttons" baCardClass="with-scroll button-panel">
<div id="flatButtons" [innerHTML]="getCode()">
</div>
</mi-card>
</div>
<div class="col-sm-6 col-xs-12">
<pre>{{getCode()}}</pre>
</div>











share|improve this question























  • What do you expect to happen?

    – Günter Zöchbauer
    Nov 7 '16 at 20:57











  • I expect the picture after "What I want:"

    – collinglass
    Nov 8 '16 at 14:52











  • Ok, so you expect the added HTML to become actual Angular components. That doesn't work this way. See my answer.

    – Günter Zöchbauer
    Nov 8 '16 at 14:53











  • this worked for me: stackoverflow.com/a/43862386/1048800

    – jgatjens
    May 9 '17 at 6:16











  • Maybe this can help: github.com/apoterenko/angular2-dynamic-component

    – Eusthace
    May 19 '17 at 0:59














8












8








8


1






I'm creating docs for a component library. I want 1 string of html that generates both the component on the page and the docs for it.



What I want:



enter image description here



What I have:



enter image description here



When I inspect the HTML, my-button tags are not present. They are being stripped out when I use innerHTML.



My component code:



private flatButtons = `<div class="button-wrapper">
<my-button [type]="'default'" [raised]="false">Default</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'primary'" [raised]="false">Primary</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'success'" [raised]="false">Success</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'info'" [raised]="false">Info</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'warning'" [raised]="false">Warning</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'danger'" [raised]="false">Danger</my-button>
</div>`

constructor() {}

getCode() {
return html_beautify(this.flatButtons, this.options)
}


My HTML template:



<div class="row">
<div class="col-sm-6 col-xs-12">
<mi-card title="Flat Buttons" baCardClass="with-scroll button-panel">
<div id="flatButtons" [innerHTML]="getCode()">
</div>
</mi-card>
</div>
<div class="col-sm-6 col-xs-12">
<pre>{{getCode()}}</pre>
</div>











share|improve this question














I'm creating docs for a component library. I want 1 string of html that generates both the component on the page and the docs for it.



What I want:



enter image description here



What I have:



enter image description here



When I inspect the HTML, my-button tags are not present. They are being stripped out when I use innerHTML.



My component code:



private flatButtons = `<div class="button-wrapper">
<my-button [type]="'default'" [raised]="false">Default</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'primary'" [raised]="false">Primary</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'success'" [raised]="false">Success</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'info'" [raised]="false">Info</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'warning'" [raised]="false">Warning</my-button>
</div>
<div class="button-wrapper">
<my-button [type]="'danger'" [raised]="false">Danger</my-button>
</div>`

constructor() {}

getCode() {
return html_beautify(this.flatButtons, this.options)
}


My HTML template:



<div class="row">
<div class="col-sm-6 col-xs-12">
<mi-card title="Flat Buttons" baCardClass="with-scroll button-panel">
<div id="flatButtons" [innerHTML]="getCode()">
</div>
</mi-card>
</div>
<div class="col-sm-6 col-xs-12">
<pre>{{getCode()}}</pre>
</div>








angular innerhtml






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 '16 at 20:21









collinglasscollinglass

3401723




3401723













  • What do you expect to happen?

    – Günter Zöchbauer
    Nov 7 '16 at 20:57











  • I expect the picture after "What I want:"

    – collinglass
    Nov 8 '16 at 14:52











  • Ok, so you expect the added HTML to become actual Angular components. That doesn't work this way. See my answer.

    – Günter Zöchbauer
    Nov 8 '16 at 14:53











  • this worked for me: stackoverflow.com/a/43862386/1048800

    – jgatjens
    May 9 '17 at 6:16











  • Maybe this can help: github.com/apoterenko/angular2-dynamic-component

    – Eusthace
    May 19 '17 at 0:59



















  • What do you expect to happen?

    – Günter Zöchbauer
    Nov 7 '16 at 20:57











  • I expect the picture after "What I want:"

    – collinglass
    Nov 8 '16 at 14:52











  • Ok, so you expect the added HTML to become actual Angular components. That doesn't work this way. See my answer.

    – Günter Zöchbauer
    Nov 8 '16 at 14:53











  • this worked for me: stackoverflow.com/a/43862386/1048800

    – jgatjens
    May 9 '17 at 6:16











  • Maybe this can help: github.com/apoterenko/angular2-dynamic-component

    – Eusthace
    May 19 '17 at 0:59

















What do you expect to happen?

– Günter Zöchbauer
Nov 7 '16 at 20:57





What do you expect to happen?

– Günter Zöchbauer
Nov 7 '16 at 20:57













I expect the picture after "What I want:"

– collinglass
Nov 8 '16 at 14:52





I expect the picture after "What I want:"

– collinglass
Nov 8 '16 at 14:52













Ok, so you expect the added HTML to become actual Angular components. That doesn't work this way. See my answer.

– Günter Zöchbauer
Nov 8 '16 at 14:53





Ok, so you expect the added HTML to become actual Angular components. That doesn't work this way. See my answer.

– Günter Zöchbauer
Nov 8 '16 at 14:53













this worked for me: stackoverflow.com/a/43862386/1048800

– jgatjens
May 9 '17 at 6:16





this worked for me: stackoverflow.com/a/43862386/1048800

– jgatjens
May 9 '17 at 6:16













Maybe this can help: github.com/apoterenko/angular2-dynamic-component

– Eusthace
May 19 '17 at 0:59





Maybe this can help: github.com/apoterenko/angular2-dynamic-component

– Eusthace
May 19 '17 at 0:59












2 Answers
2






active

oldest

votes


















5














Angular doesn't process HTML added dynamically, it just adds it verbatim except some sanitization to prevent security issues.
See In RC.1 some styles can't be added using binding syntax for how to prevent the sanitizer of stripping the HTML.



You can use ViewContainerRef.createComponent() like shown in Angular 2 dynamic tabs with user-click chosen components to add components dynamically.



There are also solutions available how to create components dynamically like shown in Equivalent of $compile in Angular 2






share|improve this answer

































    0














    I'll accept Gunter's answer since it answers my question.



    For those who are interested, the way I solved my problem was by creating a component and requiring it.



    I created a dumb component:



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


    @Component({
    selector: 'flat-buttons',
    template: require('./flatButtons.html'),
    })
    export class FlatButtons {

    constructor() {
    }
    }


    And then my modified html template:



    <div class="col-sm-6 col-xs-12">
    <mi-card title="Flat Buttons" baCardClass="with-scroll button-panel">
    <flat-buttons></flat-buttons>
    </mi-card>
    </div>
    <div class="col-sm-6 col-xs-12">
    <h3>Code Snippet</h3>
    <div class="well">
    <pre>{{getFlatButtons()}}</pre>
    </div>
    </div>


    And my modified component code:



    private flatButtons = require('./components/flatButtons/flatButtons.html')

    constructor() {}

    getFlatButtons() {
    return html_beautify(this.flatButtons, this.options)
    }





    share|improve this answer
























    • collinglass, how does your flatButtons.html look like? Can you share?

      – user1892775
      Sep 4 '17 at 15:14













    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',
    autoActivateHeartbeat: false,
    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%2f40473910%2fhow-to-dynamically-add-innerhtml-with-angular-2-components%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









    5














    Angular doesn't process HTML added dynamically, it just adds it verbatim except some sanitization to prevent security issues.
    See In RC.1 some styles can't be added using binding syntax for how to prevent the sanitizer of stripping the HTML.



    You can use ViewContainerRef.createComponent() like shown in Angular 2 dynamic tabs with user-click chosen components to add components dynamically.



    There are also solutions available how to create components dynamically like shown in Equivalent of $compile in Angular 2






    share|improve this answer






























      5














      Angular doesn't process HTML added dynamically, it just adds it verbatim except some sanitization to prevent security issues.
      See In RC.1 some styles can't be added using binding syntax for how to prevent the sanitizer of stripping the HTML.



      You can use ViewContainerRef.createComponent() like shown in Angular 2 dynamic tabs with user-click chosen components to add components dynamically.



      There are also solutions available how to create components dynamically like shown in Equivalent of $compile in Angular 2






      share|improve this answer




























        5












        5








        5







        Angular doesn't process HTML added dynamically, it just adds it verbatim except some sanitization to prevent security issues.
        See In RC.1 some styles can't be added using binding syntax for how to prevent the sanitizer of stripping the HTML.



        You can use ViewContainerRef.createComponent() like shown in Angular 2 dynamic tabs with user-click chosen components to add components dynamically.



        There are also solutions available how to create components dynamically like shown in Equivalent of $compile in Angular 2






        share|improve this answer















        Angular doesn't process HTML added dynamically, it just adds it verbatim except some sanitization to prevent security issues.
        See In RC.1 some styles can't be added using binding syntax for how to prevent the sanitizer of stripping the HTML.



        You can use ViewContainerRef.createComponent() like shown in Angular 2 dynamic tabs with user-click chosen components to add components dynamically.



        There are also solutions available how to create components dynamically like shown in Equivalent of $compile in Angular 2







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 23 '17 at 11:47









        Community

        11




        11










        answered Nov 7 '16 at 20:59









        Günter ZöchbauerGünter Zöchbauer

        317k66942885




        317k66942885

























            0














            I'll accept Gunter's answer since it answers my question.



            For those who are interested, the way I solved my problem was by creating a component and requiring it.



            I created a dumb component:



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


            @Component({
            selector: 'flat-buttons',
            template: require('./flatButtons.html'),
            })
            export class FlatButtons {

            constructor() {
            }
            }


            And then my modified html template:



            <div class="col-sm-6 col-xs-12">
            <mi-card title="Flat Buttons" baCardClass="with-scroll button-panel">
            <flat-buttons></flat-buttons>
            </mi-card>
            </div>
            <div class="col-sm-6 col-xs-12">
            <h3>Code Snippet</h3>
            <div class="well">
            <pre>{{getFlatButtons()}}</pre>
            </div>
            </div>


            And my modified component code:



            private flatButtons = require('./components/flatButtons/flatButtons.html')

            constructor() {}

            getFlatButtons() {
            return html_beautify(this.flatButtons, this.options)
            }





            share|improve this answer
























            • collinglass, how does your flatButtons.html look like? Can you share?

              – user1892775
              Sep 4 '17 at 15:14


















            0














            I'll accept Gunter's answer since it answers my question.



            For those who are interested, the way I solved my problem was by creating a component and requiring it.



            I created a dumb component:



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


            @Component({
            selector: 'flat-buttons',
            template: require('./flatButtons.html'),
            })
            export class FlatButtons {

            constructor() {
            }
            }


            And then my modified html template:



            <div class="col-sm-6 col-xs-12">
            <mi-card title="Flat Buttons" baCardClass="with-scroll button-panel">
            <flat-buttons></flat-buttons>
            </mi-card>
            </div>
            <div class="col-sm-6 col-xs-12">
            <h3>Code Snippet</h3>
            <div class="well">
            <pre>{{getFlatButtons()}}</pre>
            </div>
            </div>


            And my modified component code:



            private flatButtons = require('./components/flatButtons/flatButtons.html')

            constructor() {}

            getFlatButtons() {
            return html_beautify(this.flatButtons, this.options)
            }





            share|improve this answer
























            • collinglass, how does your flatButtons.html look like? Can you share?

              – user1892775
              Sep 4 '17 at 15:14
















            0












            0








            0







            I'll accept Gunter's answer since it answers my question.



            For those who are interested, the way I solved my problem was by creating a component and requiring it.



            I created a dumb component:



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


            @Component({
            selector: 'flat-buttons',
            template: require('./flatButtons.html'),
            })
            export class FlatButtons {

            constructor() {
            }
            }


            And then my modified html template:



            <div class="col-sm-6 col-xs-12">
            <mi-card title="Flat Buttons" baCardClass="with-scroll button-panel">
            <flat-buttons></flat-buttons>
            </mi-card>
            </div>
            <div class="col-sm-6 col-xs-12">
            <h3>Code Snippet</h3>
            <div class="well">
            <pre>{{getFlatButtons()}}</pre>
            </div>
            </div>


            And my modified component code:



            private flatButtons = require('./components/flatButtons/flatButtons.html')

            constructor() {}

            getFlatButtons() {
            return html_beautify(this.flatButtons, this.options)
            }





            share|improve this answer













            I'll accept Gunter's answer since it answers my question.



            For those who are interested, the way I solved my problem was by creating a component and requiring it.



            I created a dumb component:



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


            @Component({
            selector: 'flat-buttons',
            template: require('./flatButtons.html'),
            })
            export class FlatButtons {

            constructor() {
            }
            }


            And then my modified html template:



            <div class="col-sm-6 col-xs-12">
            <mi-card title="Flat Buttons" baCardClass="with-scroll button-panel">
            <flat-buttons></flat-buttons>
            </mi-card>
            </div>
            <div class="col-sm-6 col-xs-12">
            <h3>Code Snippet</h3>
            <div class="well">
            <pre>{{getFlatButtons()}}</pre>
            </div>
            </div>


            And my modified component code:



            private flatButtons = require('./components/flatButtons/flatButtons.html')

            constructor() {}

            getFlatButtons() {
            return html_beautify(this.flatButtons, this.options)
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 8 '16 at 15:02









            collinglasscollinglass

            3401723




            3401723













            • collinglass, how does your flatButtons.html look like? Can you share?

              – user1892775
              Sep 4 '17 at 15:14





















            • collinglass, how does your flatButtons.html look like? Can you share?

              – user1892775
              Sep 4 '17 at 15:14



















            collinglass, how does your flatButtons.html look like? Can you share?

            – user1892775
            Sep 4 '17 at 15:14







            collinglass, how does your flatButtons.html look like? Can you share?

            – user1892775
            Sep 4 '17 at 15:14




















            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40473910%2fhow-to-dynamically-add-innerhtml-with-angular-2-components%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

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly