如何使用angular 2组件动态添加innerHTML [英] How to dynamically add innerHTML with angular 2 components

查看:36
本文介绍了如何使用angular 2组件动态添加innerHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为组件库创建文档.我想要 1 个 html 字符串来生成页面上的组件和它的文档.

我想要的:

我有什么:

当我检查 HTML 时,我的按钮标签不存在.当我使用innerHTML时,它们被剥离了.

我的组件代码:

private flatButtons = `<div class="button-wrapper"><my-button [type]="'default'" [raised]="false">默认</my-button>

<div class="button-wrapper"><my-button [type]="'primary'" [raised]="false">Primary</my-button>

<div class="button-wrapper"><my-button [type]="'success'" [raised]="false">成功</my-button>

<div class="button-wrapper"><my-button [type]="'info'" [raised]="false">Info</my-button>

<div class="button-wrapper"><my-button [type]="'warning'" [raised]="false">警告</my-button>

<div class="button-wrapper"><my-button [type]="'danger'" [raised]="false">危险</my-button>

`构造函数(){}获取代码(){返回 html_beautify(this.flatButtons, this.options)}

我的 HTML 模板:

<div class="col-sm-6 col-xs-12"><mi-card title="平面按钮" baCardClass="with-scroll button-panel"><div id="flatButtons" [innerHTML]="getCode()">

</mi-card>

<div class="col-sm-6 col-xs-12"><pre>{{getCode()}}</pre>

解决方案

Angular 不处理动态添加的 HTML,它只是逐字添加,除了一些清理以防止安全问题.参见 在 RC.1 中一些无法使用绑定语法添加样式,以了解如何防止剥离 HTML 的消毒剂.

您可以使用 ViewContainerRef.createComponent(),如 Angular 2 动态选项卡,带有用户单击选择的组件 以动态添加组件.

还有如何动态创建组件的解决方案,如 等效于 Angular 2 中的 $compile

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 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 2组件动态添加innerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆