How to dynamically add innerHTML with angular 2 components
Asked Answered
A

2

11

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>

Almandine answered 7/11, 2016 at 20:21 Comment(5)
What do you expect to happen?Frenchy
I expect the picture after "What I want:"Almandine
Ok, so you expect the added HTML to become actual Angular components. That doesn't work this way. See my answer.Frenchy
this worked for me: https://mcmap.net/q/470507/-angular2-innerhtml-angular2-tag-doesn-39-t-workDryer
Maybe this can help: github.com/apoterenko/angular2-dynamic-componentIrrelevant
A
6

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

Angeles answered 7/11, 2016 at 20:59 Comment(0)
A
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)
}
Almandine answered 8/11, 2016 at 15:2 Comment(1)
collinglass, how does your flatButtons.html look like? Can you share?Arid

© 2022 - 2024 — McMap. All rights reserved.