typescript 如何获取 Angular 2 组件的 html 模板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40751789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:01:58 来源:igfitidea点击:
How to get html template of an Angular 2 Component?
提问by swapp1990
I have a simple angular 2 component:
我有一个简单的 angular 2 组件:
@Component({ selector: 'test-text', template: `<p> Text </p>` }) export class TestComponent { constructor() {} getHtmlContent() {return;} //This should return '<p> Text </p>' as a string }
@Component({ selector: 'test-text', template: `<p> Text </p>` }) export class TestComponent { constructor() {} getHtmlContent() {return;} //This should return '<p> Text </p>' as a string }
What is the simplest way I can the html content for my Component back?
我可以返回组件的 html 内容的最简单方法是什么?
回答by tjoskar
You can use ElementRef
. E.g.
您可以使用ElementRef
. 例如
import { Component, ElementRef } from '@angular/core';
@Component({
selector: 'test-text',
template: `<p> Text </p>`
})
export class TestComponent {
elRef: ElementRef
constructor(elRef: ElementRef) {
this.elRef = elRef;
}
getHtmlContent() {
//This will return '<p> Text </p>' as a string
return this.elRef.nativeElement.innerHTML;
}
}