4

I am very new to Angular, I am trying to insert the html file as my string and insert into DIV element

I have my search.component.html called

<div #ID></div>

components.ts

import { Component} from '@angular/core';

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.scss']
})
export class SearchComponent {
  constructor() {}

  let ServerResponseHtml = '<div><input type="text"/><input type="text"/><span class="btn btn-outline-primary btn-sm" (click)="open(content)">View Document</span></div>';

  document.getElementById("DIV").innerHTML = ServerResponseHtml;
}

I am getting the response from server as complete html markup, Just I need to append into my DOM and display the content, the markup can have inline styles also.

I tried for <div [innerHTML]="ServerResponseHtml"></div> and <div innerHTML="{{ServerResponseHtml}}"></div> but this is not displaying as html it is displayed as text.

5
  • I think you're not doing this the right way on a global scale. You should write your resulting div in the template with dynamic variables and maybe a condition for display. Commented Jan 19, 2018 at 10:37
  • can you check this stackoverflow.com/questions/43682801/… Commented Jan 19, 2018 at 10:38
  • Wait, you just want to insert the content of an .html file from the server to your client? Commented Jan 19, 2018 at 10:48
  • Yes, I want to insert the child elements, as Text format we can use variable {{dynamic}}. I need to render those in my page. Commented Jan 19, 2018 at 11:44
  • @swarooppallapothu will this work for (click) events as asked in the original question? Commented Apr 6, 2021 at 14:39

1 Answer 1

8

We need to use the safehtml for displaying the html.

  1. We need to create the Pipe for this. safe-html-pipe.ts
    import {DomSanitizer, SafeHtml} from '@angular/platform-browser';
    import {Pipe, PipeTransform} from '@angular/core';
    
    @Pipe({name: 'safehtml'})
    
    export class SafeHtmlPipe implements PipeTransform {
      constructor(private sanitized: DomSanitizer) {
      }
    
      transform(value): SafeHtml {
        return this.sanitized.bypassSecurityTrustHtml(value);
      }
    }
  1. component.ts We need to import the pipe

import {Component, NgModule, Pipe, PipeTransform} from '@angular/core'

import {BrowserModule} from '@angular/platform-browser'
import { FormsModule } from '@angular/forms';
import { DomSanitizer } from '@angular/platform-browser'
import { SafeHtmlPipe } from './safe-html-pipe';
@Component({
selector: 'app-root',
template: 
    `<div [innerHtml]="safeHtmlContent | safehtml">
 </div>"})`
 
export class AppComponent {
 name:string;
  safeHtmlContent : string;
  constructor() {
    this.name = 'Angular2'
    this.safeHtmlContent  = '<html><head><title>Hello safe</title></head><body><h1>hello world Hello Umesh</h1></body></html>';
  }
}

Hope this helps :).

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

1 Comment

Will [innerHtml] process (click) also? as asked in the original question. I doubt. Why is this an accepted answer?

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.