Jason Goemaat
Jason Goemaat

Reputation: 29214

How do I escape curly braces for display on page when using AngularJS?

I want the user to see double curly braces, but Angular binds them automatically. This is the opposite case of this question where they want to not see curly braces used for binding when the page is loading.

I want the user to see this:

My name is {{person.name}}.

But Angular replaces {{person.name}} with the value. I thought this might work, but angular still replaces it with the value:

{{person.name}}

Plunker: http://plnkr.co/edit/XBJjr6uR1rMAg3Ng7DiJ

Upvotes: 112

Views: 56996

Answers (8)

user369142
user369142

Reputation: 2915

From Angular compiler:

Unexpected character "EOF" (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.)

So in the original question - you would end up with:

My name is {{ '{' }}{{ '{' }}person.name{{ '}' }}{{ '}' }}

Upvotes: 0

bhavya_karia
bhavya_karia

Reputation: 780

I wanted single brackets across text and the above solutions didn't work for me. So did want the Angular recommended.

Angular Version: 5

Required Text: My name is {person.name}.

<span>My name is {{'{'}}person.name{{'}'}}.</span>

I hope it helps someone.

Upvotes: 5

Matt
Matt

Reputation: 35231

Updated for Angular 9

Use ngNonBindable to escape interpolation binding.

<div ngNonBindable>
  My name is {{person.name}}
</div>

Upvotes: 12

Mike Pugh
Mike Pugh

Reputation: 6787

<code ng-non-bindable>{{person.name}}</code>

Documentation @ ngNonBindable

Upvotes: 160

EscapeNetscape
EscapeNetscape

Reputation: 2939

<span>{{</span>{{variable.name}}<span>}}</span>

Upvotes: 3

Nabi K.A.Z.
Nabi K.A.Z.

Reputation: 10714

Use ng-non-bindable in container, this is effective on all element inside here container.

<div ng-non-bindable>
  <span>{{person.name}}</span>
  <img src="#" alt="{{person.name}}">
  <input placeholder="{{person.name}}">
</div>

Upvotes: 3

joeytwiddle
joeytwiddle

Reputation: 31285

In our case we wanted to present curly brackets in a placeholder, so they needed to appear inside an HTML attribute. We used this:

 <input placeholder="{{ 'Hello {' + '{person.name}' + '}!' }}" ...>

As you can see, we are building up a string from three smaller strings, in order to keep the curly braces separated.

'Hello {' + '{person.name}' + '}!'

This avoids using ng-non-bindable so we can continue to use ng- attributes elsewhere on the element.

Upvotes: 15

bh_earth0
bh_earth0

Reputation: 2818

Edit: adding \ slash between brackets inside the quotes works

{{  "{{ person.name }\}"   }}  

this too .. by passes angular interpreting

{{ person.name }<!---->}

this too ..

{{ person.name }<x>} 

{{ person.name }<!>}

Upvotes: 31

Related Questions