7

I am working on an Angular 5 project at the moment. I am facing a tricky issue here.

I want to include some code snippets in my component.html file, through the use of the <code> tag. But doing so seems to return an processing error on the angular side. So I tried to put the code in variable on the *.component.ts file, and using data binding, display the code like this:

<code_tag>{{my_code_snippet}}</code_tag>

But now the issue is that the code always comes out as a string, and not properly formatted (for example if it is JSON code)

How should I tackle this?

4
  • 1
    Have you tried <pre>{{my_code_snippet}}</pre> tag ? Commented Mar 13, 2018 at 11:27
  • 1
    did you try this [innerHTML]="my_code_snippet" on the code container? Commented Mar 13, 2018 at 11:28
  • Hi, I have added the <pre> tag an it seems to be working. Thanks. [InnerHTML] also works fine. Commented Mar 13, 2018 at 11:35
  • I have used the <pre> tag, it is not working even after that Commented May 29, 2018 at 6:51

1 Answer 1

1

You can tell Angular to ignore strings in the <code> element with the ngNonBindable attribute

<code ngNonBindable>
    console.log("no output");
</code>
Sign up to request clarification or add additional context in comments.

1 Comment

You may still have issues displaying curly braces because of this: github.com/angular/angular/issues/11859

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.