0

I create <mat-checkbox> elements dynamically from incoming data that fetched by API.

component.ts:

this.content = sanitizer.bypassSecurityTrustHtml(`<mat-checkbox>test</mat-checkbox>`)

component.html

<div [innerHTML]="content"></div>

but the checkbox input is not rendered, the same issue occures if I used <input type="checkbox"> instead of `

and it rendered correctly if I added <mat-checkbox statically to component.html

please check this repo: https://stackblitz.com/edit/angular-8heckn

note:

this.content is a string, not an object. because I want to add extra spaces (tabs) before each child input.

for instance:

  • programming
    • front-end
      • Angular
      • React
      • Vue
    • back-end
      • node.js
      • express.js
      • php
Sh eldeeb
  • 1,589
  • 3
  • 18
  • 41

1 Answers1

0

"Angular doesn't process HTML added dynamically, it just adds it verbatim except some sanitization to prevent security issues." [see Stackoverflow question How to dynamically add innerHTML with angular 2 components]

To learn about adding Angular components dynamically, see the following:

Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • but I have some data (object) not a component, whitch I want to inject them inside the current component. @Christopher Peisert – Sh eldeeb Feb 16 '20 at 19:48