How to get html template of Angular 2 component?

I have a simple angular 2 component:

@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 easiest way to return html content for my component?

+5
source share
1 answer

You can use ElementRef . For instance.

 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; } } 
+6
source

Source: https://habr.com/ru/post/1260166/


All Articles