0

I have a string that contains the following HTML code:

             <div>
                <TextSubtitle [color]="'#B3B4B4'" [bold]="'true'">Datos Personales del Solicitante</TextSubtitle>
                <div style="margin-top: 20px;display: flex;justify-content: space-between">
                    <InputItem style="width: 47%" [label]="'Primer nombre *'"></InputItem>
                    <InputItem style="width: 47%" [label]="'Segundo nombre *'"></InputItem>
                </div>
                <div style="margin-top: 10px;display: flex;justify-content: space-between">
                    <InputItem style="width: 47%" [label]="'Apellido paterno *'"></InputItem>
                    <InputItem style="width: 47%" [label]="'Apellido materno *'"></InputItem>
                </div>
                <div style="margin-top: 10px;display: flex;justify-content: space-between">
                    <InputItem style="width: 47%" [label]="'Fecha de nacimiento *'"></InputItem>
                    <InputItem style="width: 47%" [label]="'Género *'"></InputItem>
                </div>
                <div style="margin-top: 10px;display: flex;justify-content: space-between">
                    <InputItem style="width: 47%" [label]="'Ocupación *'"></InputItem>
                    <InputItem style="width: 47%" [label]="'Dependientes económicos *'"></InputItem>
                </div>
                <div style="margin-top: 10px;display: flex;justify-content: space-between">
                    <InputItem style="width: 47%" [label]="'Estado civil *'"></InputItem>
                    <InputItem style="width: 47%" [label]="'Número de hijos *'"></InputItem>
                </div>
                <div style="margin-top: 10px;display: flex;justify-content: space-between">
                    <InputItem style="width: 47%" [label]="'RFC *'"></InputItem>
                    <InputItem style="width: 47%" [label]="'CURP *'"></InputItem>
                </div>
            </div> 

note: InputItem is a angular component I build

I am trying to insert the entire string as HTML code in my HTML file

typescript file:

ngOnInit() {
    this.doc = new DOMParser().parseFromString(myString, "text/html");
  }

HTML file:

<div>
    {{doc.firstChild.innerHTML}}
</div>

this will print the actually HTML code as a string on screen.

1 Answer 1

1

You need to use DomSanitizer and tell angular your content is safe with bypassSecurityTrustHtml method. Also you need to replace firstChild.innerHTML with doc.body.outerHTML if you would like to render the whole document.

Here is the sample on StackBlitz

Sign up to request clarification or add additional context in comments.

2 Comments

[innerHTML]= will only display the first html element with no styles
@I'mnothuman sure, it's because you use firstChild.innerHTML. Please, see my updated answer with an example on stackblitz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.