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

后端 未结 8 1441
醉话见心
醉话见心 2020-11-27 02:28

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 fo

相关标签:
8条回答
  • 2020-11-27 02:59

    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>
    
    0 讨论(0)
  • 2020-11-27 03:00
    <span>{{</span>{{variable.name}}<span>}}</span>
    
    0 讨论(0)
  • 2020-11-27 03:01

    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.

    0 讨论(0)
  • 2020-11-27 03:08

    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.

    0 讨论(0)
  • 2020-11-27 03:09
    <code ng-non-bindable>{{person.name}}</code>
    

    Documentation @ ngNonBindable

    0 讨论(0)
  • 2020-11-27 03:11

    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{{ '}' }}{{ '}' }}
    
    0 讨论(0)
提交回复
热议问题