3

This question is a continuity to a previous question that I asked: previous question

What I wanted to do is to pass an HTML code to an angular component so that it can be displayed and executed. The Idea was to pass the HTML code to the component as a string input (after escaping all the special characters), anf then using pipes to bypass the navigator's trust security so that the code can be executed.

However, if the HTML code is long, it can be very tedious to pass it as a string and to escape all the special characters. My question is : Is there any way to pass my HTML code without passing by inputs ? If not, Is there any input type that can be more suitable for an HTML code than a string ?

Thanks for your help.

2
  • You can use content projection. Commented Aug 23, 2019 at 10:31
  • You can create a component and add it's tag in another component. Commented Aug 23, 2019 at 10:33

2 Answers 2

5

You can use <ng-content></ng-content>. If you put ng-content in the template of a componenet you can insert html code there. It works like this:

Template of component where code should be put

<div>
  <ng-content></ng-content>
</div>

now you can just put the code between the tag of the component

<example-component>
  <p>Example Code</p>
  <p>More example code</p>
</example-component>

The two <p> will be inserted where the <ng-content> tag is

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

Comments

4

You can simply pass the string as shown<div [innerHTML]="yourStringBindingHere"></div>

1 Comment

that's what i'm doing right now, but still all the special characters in the string should be escaped...for a long code it's problematic

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.