简体   繁体   English

如何从Angular2模板(的一部分)生成html?

[英]How can I generate html from (part of) an Angular2 template?

I need to allow my end users to copy/paste the html output of a component template into eg a MailChimp template or their own website, as static html. 我需要允许我的最终用户将组件模板的html输出复制/粘贴到例如MailChimp模板或他们自己的网站中,作为静态html。 I need something similar to the buttons that generate embed-able iframe code on some content management websites, so the user can paste that iframe html into the appropriate part of their own site. 我需要与在某些内容管理网站上生成可嵌入iframe代码的按钮类似的东西,以便用户可以将iframe html粘贴到自己网站的相应部分。

My (simplified) component template with a section that looks like this: 我的(简化的)组件模板的部分如下所示:

<div *ngFor="let item of items">
  <p>{{item.title}}</p>
</div>

...which at runtime should create html in the DOM that looks something like this: ...在运行时应在DOM中创建如下所示的html:

<p>item title 1</p>
<p>item title 2</p>
<p>item title 3</p>
.
.

I want to give my users a button that will copy that generated html to their clipboard, so they can paste and display it eg in the appropriate section of their own static website. 我想给我的用户一个按钮,它将生成的html复制到他们的剪贴板,以便他们可以粘贴和显示它,例如在他们自己的静态网站的适当部分。 I already have a custom copy-to-clipboard directive that will handle the actual copy operation on any string I pass to it, but in this case what should I be copying? 我已经有一个自定义的copy-to-clipboard指令,它将处理传递给它的任何字符串的实际复制操作,但是在这种情况下,我应该复制什么? How do I access the 'static' version of the html " <p>item title 1</p>... " that the user sees rendered in the browser? 如何访问用户在浏览器中看到的呈现为HTML的“静态”版本的“ <p>item title 1</p>... ”?

Please let me know if my question needs additional clarification. 如果我的问题需要进一步澄清,请告诉我。 Google results seem to indicate this is not a common operation, so I'm having trouble expressing the question with the proper terminology. Google的结果似乎表明这不是一个常见的操作,因此我很难用适当的术语来表达这个问题。

Use & lt; 使用&lt; and & gt;just has I have used below. &gt;我在下面使用过。

<div *ngFor="let item of items">
  &lt;p&gt;{{item.title}}&lt;/p&gt;
</div>

Try converting title in items variable in your component to be like this: 尝试将组件中items变量中的标题转换为如下形式:

items = [{title: "<p>item title 1</p>"}, {title: "<p>item title 2</p>"}, {title: "<p>item title 3</p>"}]

Then redo your code: 然后重做您的代码:

<div *ngFor="let item of items">
   {{item.title}}
</div>

You can achieve it using a custom directive in your component: 您可以在组件中使用自定义指令来实现它:

import { Directive, ElementRef} from '@angular/core';

@Directive({
  selector: '[myInnerHtml]'
})
export class MyInnerHtmlDirective {
  constructor(private _el: ElementRef) {}
  getInnerHtml(): string {
    return this._el.nativeElement.innerHTML;
  }
}

using inside component template: 使用内部组件模板:

<div (click)="click()" myInnerHtml>
  <p *ngFor="let item of items">{{item}}</p>
</div>

Component: 零件:

import { Component, ViewChildren } from '@angular/core';
import { MyInnerHtmlDirective } from '../my-first.directive';

@Component({
  selector: 'test',
  templateUrl: './test.component.html'
})
export class TestComponent {
  @ViewChildren(MyInnerHtmlDirective) directives;
  constructor() { }
  items: string[] = ['item title 1', 'item title 2', 'item title 3']

  click() {
    let html: string = (<MyInnerHtmlDirective>this.directives.first).getInnerHtml();
    let cleanHmtl: string = html.replace(/<!--[\s\S]*?-->/g, ''); // deleting comments
    console.log(cleanHmtl);
  }
}

It should give you <p>item title 1</p><p>item title 2</p><p>item title 3</p> 它应该给您<p>item title 1</p><p>item title 2</p><p>item title 3</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM