2

I have HTML code on Component variable like below :

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

@Component({
  selector: 'app-root',
  template: `Hi <div [innerHTML]="name"></div>`,
  styleUrls: ['./app.component.css'],
   styles: [`
    .highlight {
      background-color: yellow;
    }
  `],
})
export class AppComponent {
name :string ="dude ! <input type='text'/>";
}

It shows the output like "Hi dude !" But no text box there.How can I display the text box bind using component variable ?

2
  • What does it show in the browser if you inspect it? Commented Aug 10, 2017 at 18:52
  • there is no text box ctrl code on inspecting in the browser, simply "Hi <div> dude </div>" Commented Aug 10, 2017 at 18:55

1 Answer 1

3

This code is not secure. So, rendering input elements is disallowed by default. You need to use DomSanitizer to allow it:

constructor(sanitizer: DomSanitizer) {
  this.name = sanitizer.bypassSecurityTrustHtml( this.name);
}

See the plunker sample that illustrates this.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.