2

I want to show some example Angular code and HTML in my angular page, but whatever I do, Angular tries to parse it.

I've tried in <code>, in <pre> and even this: &lt;div class="name">&#123;&#123; name &#125;&#125; &lt;/div>

but even then it tries to bind to 'name'.

I don't want to do it via any binding, I just want to type the HTML in the components HTML page.

Maybe a better example: when I paste this text in the HTML, I still get an input element, I just want to show the HTML (from input to box.value).

<pre><code><input #box (keyup.enter)="onEnter(box.value)"></code></pre>

1

2 Answers 2

2

You can make a variable in the component file

public htmlSnippt = `<input #box (keyup.enter)="onEnter(box.value)">`;

Then use it in the html

<pre>
   {{htmlSnippt}}
</pre>

in XHTML you can use CDATA

<![CDATA[<input #box (keyup.enter)="onEnter(box.value)">]]>
Sign up to request clarification or add additional context in comments.

3 Comments

Is there also a way that I can type it directlly in the HTML?
Nice, this works in a <pre> but then I loose the code formatting. When I put it in <code> it shows the code formatting but I loose the HTML tags....
you can CDATA as pure XHTML afaik, check the answer again @Michel
0

Try this:

<pre>
  <code [innerHTML]="someCode"></code>
</pre>

Component:

export class SomeComponent {
  someCode = 'export model = new Model({
     a:function(){}
     b:4
  })'
}

Update to properly should html tags

<pre>
  <code>{{someCode}}</code>
</pre>

<pre>
  <code>{{someOtherCode}}</code>
</pre>

Component

export class SomeComponent  {
  someCode = 'export model = new Model({a:function(){}b:4})'
  someOtherCode = '<div>some html</div>'
}

2 Comments

this doesn't work when I add a <DIV> tag in the code.
Now it works even with html tags. See updated answer

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.