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

7
  • 1
    Possible duplicate of Angular 2 binding/events not working when adding template as innerHTML Commented Oct 29, 2018 at 14:26
  • @saran why you use [innerHTML] instead of doing in a simple way... ? Commented Oct 30, 2018 at 3:04
  • @saran Are you able to view text box in HTML page ? Commented Oct 30, 2018 at 9:58
  • I am able to see text box, but i can't edit the text box, that's a different problem Commented Oct 30, 2018 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 ... Commented Oct 30, 2018 at 17:20

0

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.