0

I would like to show some angular code on web page like

      import { Pipe, PipeTransform } from '@angular/core';

      @Pipe({name: 'reverseStr'})
      export class ReverseStr implements PipeTransform {
          transform(value: string): string {
          let newStr: string = "";
          for (var i = value.length - 1; i >= 0; i--) {
              newStr += value.charAt(i);
          }
          return newStr;
          }
      }

If I just add it as it is (or wrap with ordinary html tags), it breaks the app. How to fix it? Also can I add language-related coloring to the snippet?

2 Answers 2

1

You'll need to 'escape' the curly braces.

Example:

            <code>
              import {{ '{' }} Pipe, PipeTransform {{ '}' }} from '@angular/core';

              @Pipe({{ '{' }}name: 'reverseStr'{{ '}' }})
              export class ReverseStr implements PipeTransform {{ '{' }}
                transform(value: string): string {{ '{' }}
                let newStr: string = "";
                for (var i = value.length - 1; i >= 0; i--) {{ '{' }}
                    newStr += value.charAt(i);
                    {{ '}' }}
                return newStr;
                {{ '}' }}
                {{ '}' }}
            </code>

Alternatively, you can use character codes:

&#123; = { &#125; = }

code source: https://ascii.cl/htmlcodes.htm

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

Comments

0

You want the normal {{ code }} syntax which will encode the variable for displaying.

Adding code and a pre will style it the way you want (or you can do the same through CSS by setting the CSS of the container to have white-space:pre)

<div><code><pre>{{code}}</pre></code></div>

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.