How to dynamically add innerHTML with angular 2 components
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:

What I have:

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
add a comment |
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:

What I have:

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
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
add a comment |
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:

What I have:

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

What I have:

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
angular innerhtml
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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)
}
collinglass, how does your flatButtons.html look like? Can you share?
– user1892775
Sep 4 '17 at 15:14
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
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
add a comment |
add a comment |
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)
}
collinglass, how does your flatButtons.html look like? Can you share?
– user1892775
Sep 4 '17 at 15:14
add a comment |
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)
}
collinglass, how does your flatButtons.html look like? Can you share?
– user1892775
Sep 4 '17 at 15:14
add a comment |
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)
}
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)
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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