Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you display a literal {{ in Angular 2

Tags:

angular

I have a template in Angular2 rc 1 where I need to display a chunk of code from a different language. That language has {{ in it.

import {Component, Input} from '@angular/core';

@Component({
    selector: 'blue-box',
    template: `
        <div class="blue-box">
            <div class="blue-box-title">{{boxTitle}}</div>
            {{define "bob"}}This is the template bob{{end}}
            <span>
                <ng-content></ng-content>
            </span>
        </div>
    `,
    styleUrls: ['../css/blue-box.css'],
})
export class BlueBox {

    @Input() boxTitle: string;

    constructor() {
    }

}

How do I get the template processor to treat {{ as a literal string instead of the start of a template expression? The problem occures at the {{define "bob"}} where I need a literal {{.

The error on the browser (chrome) console is:

EXCEPTION: Template parse errors:
Parser Error: Unexpected token 'bob' at column 8 in [
        {{define "bob"}}This is the template bob{{end}}
        ] in BlueBox@2:49 ("
    <div class="blue-box">
        <div class="blue-box-title">{{boxTitle}}</div>[ERROR ->]
        {{define "bob"}}This is the template bob{{end}}
        <span>
            <ng-content></ng-content>
 "): BlueBox@2:49
like image 997
philipschlump Avatar asked Jun 01 '16 20:06

philipschlump


1 Answers

use ngNonBindable

example:

<div ngNonBindable> {{ I'm inside curly bracket }} </div>

UPDATE

The above is valid in Anuglar 2, now in Angular 4 there is a class called DomSanitizer that can be used for inserting any kind of code inside of your HTML as a text.

You can check this working plunker from micronyks's answer

like image 105
Khaled Al-Ansari Avatar answered Oct 19 '22 08:10

Khaled Al-Ansari