Is it possible to add the " *ngIf " directive to an element created with TypeScript?
The only solution that I found yet is, depending on the condition, this element dynamically created changes it’s display between ‘none’ or ‘block’.
Tried with ‘element.setAttribute()’ but doesn’t work.
Tried with creating an element and setting it’s ‘innerHTML’ like this:
element.innerHTML = `
<child *ngIf="condition"> ... </child>
`
Used the tag ‘child’ as a representation of a random element.
Also didn’t work.
Thanks in advance!
2
Answers
You are already creating dynamic elements on certain condition then what is the exact usecase for adding *ngIf directive on those elements? It would be helful if you share insights on the use case.
Ok Thanks for the use case.
Can you check the below code:
Your HTML string inside the innterHTML would not be parsed as the angular compiler only compiles templates at runtime when declared in the template property or external template file.
You can try running your condition in your TS file.