1

I have a component ts file like below

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

@Component({
  template: '<div [innerHtml]='htmlContent'></div>'
})

export class DesignComponent implements OnInit {

modelValue='abcd';

htmlContent='<input type="text" [(ngModel)]="modelValue" name="model"> ';

constructor(){}

}

In my template, I am inserting html using innerHtml method like below

<div [innerHtml]='htmlContent'></div>

So, it is displaying output as input box, but ngModel values not rendering into the input box.

I tried using sanitize also, Still model value not inserting into input box.

Sanitize tried in 2 ways like,

this.htmlContent= this.sanitizer.bypassSecurityTrustHtml(htmlContent);
this.htmlContent= this.sanitizer.sanitize(SecurityContext.HTML, htmlContent);

How to achieve this using angular2/4/5?

Thanks in advance

saran
  • 75
  • 3
  • 13
  • 1
    Possible duplicate of [Angular 2 binding/events not working when adding template as innerHTML](https://stackoverflow.com/questions/37901436/angular-2-binding-events-not-working-when-adding-template-as-innerhtml) – ConnorsFan Oct 29 '18 at 14:26
  • @saran why you use [innerHTML] instead of doing in a simple way... ? – Sachin Shah Oct 30 '18 at 03:04
  • @saran Are you able to view text box in HTML page ? – Sachin Shah Oct 30 '18 at 09:58
  • I am able to see text box, but i can't edit the text box, that's a different problem –  saran Oct 30 '18 at 12:18
  • @saran Till I have tried in my experience If you will set form then also you will not be able to get button click event ... I think you should think some other option. If you can show how you got data from backend then I can help you ... – Sachin Shah Oct 30 '18 at 17:20
  • @Sachin, After service call i'm getting bunch of html code which contains images, div, input boxes with values and models , user can edit the page and he can save that as a new page with new value, this process will continue many time, so dynamic html code i can't write directly in template, so through some way i have to render it. –  saran Oct 31 '18 at 08:41
  • did you get the answer of your question? – surendra kumar Sep 05 '21 at 14:19

0 Answers0