Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display code within <code>-tag in angular2?

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(){}
 })`
like image 442
Jan Fanslau Avatar asked Oct 19 '16 13:10

Jan Fanslau


People also ask

How do I display code snippets in HTML?

Using pre & code tag The <code> tag is used to define a piece of computer code. The content inside is displayed in the browser's default monospace font. So, by using <pre> & <code>, you can display a code snippet such as function.

What is snippet in angular?

We are glad to introduce Angular Snippets, a utility for adding Syncfusion Angular components in the HTML file of an Angular application. This utility is available in our 2021 Volume 3 release. This code snippet utility saves significant time for developers in adding Syncfusion Angular components to their projects.

What is HTML in Angular?

An Angular HTML template renders a view, or user interface, in the browser, just like regular HTML, but with a lot more functionality. When you generate an Angular application with the Angular CLI, the app. component. html file is the default template containing placeholder HTML.


1 Answers

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.

like image 80
Günter Zöchbauer Avatar answered Sep 30 '22 07:09

Günter Zöchbauer