7

I'd like to show code snippets within my angular2 application. But I am not able to paste simple javascript code within a code-tag.

I always need to add a second curly bracket and I have to add ngNonBindable. But I don't want to add a second bracket.

Has anyone a solution to this?

http://plnkr.co/edit/OzlHLJxgwSvUffryV3e4

this is my app.component.html:

<h1>Here is the documentation for the model:</h1>
<pre>
  <code ngNonBindable>
    export model = new Model({
      a:1,
      b:function(){}
    })
  </code>
</pre>

The User should see:

here is the documentation for the model:

export model = new Model({
     a:1,
     b:function(){}
 })`
2
  • did you try <code ngNonBindable [innerHtml]='model'></code> ? Commented Oct 19, 2016 at 13:33
  • yes, but i'd like to have it within the template, to have more comfort, instead of having a lot of variables with the code-content Commented Oct 19, 2016 at 14:18

2 Answers 2

7

I think the simplest way is to use [innerHTML]="xxx" and hold the code in the components class

<pre>
  <code [innerHTML]="code"></code>
</pre>
export class AppComponent {
  code = `
export model = new Model({
  a:1,
  b:function(){}
})
`
}

Plunker example

In RC.1 some styles can't be added using binding syntax might also be helpful.

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

3 Comments

Well, yes, that is a possible way, but I plan to have a lot of code-snippets within the template. It would be more comfortable to have it directly within the template file.
I guess then you need to escape every { and every } as {{'{'}} and {{'}'}}. There is an open issue as far as I remember to make ngNonBindable work here.
thanx, I will use your solution above.
1

I was having the same issue, and ended up replacing all curly braces with a numerical character reference. e.g:

<pre>
  <code>
    export model = new Model(&#123;
      a:1,
      b:function()&#123;&#125;
    &#125;)
  </code>
</pre>

Plunker Example

Comments

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.