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

Angularjs

Angularjs Problem Overview


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

Angularjs Solutions


Solution 1 - Angularjs

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

Documentation @ ngNonBindable

Solution 2 - Angularjs

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 }<!>}

Solution 3 - Angularjs

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.

Solution 4 - Angularjs

Updated for Angular 9

Use ngNonBindable to escape interpolation binding.

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

Solution 5 - Angularjs

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>

Solution 6 - Angularjs

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

Solution 7 - Angularjs

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.

Solution 8 - Angularjs

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{{ '}' }}{{ '}' }}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJason GoemaatView Question on Stackoverflow
Solution 1 - AngularjsMike PughView Answer on Stackoverflow
Solution 2 - Angularjsbh_earth0View Answer on Stackoverflow
Solution 3 - AngularjsjoeytwiddleView Answer on Stackoverflow
Solution 4 - AngularjsMattView Answer on Stackoverflow
Solution 5 - AngularjsNabi K.A.Z.View Answer on Stackoverflow
Solution 6 - AngularjsEscapeNetscapeView Answer on Stackoverflow
Solution 7 - Angularjsbhavya_kariaView Answer on Stackoverflow
Solution 8 - Angularjsuser369142View Answer on Stackoverflow