16

Can I bind multiple values using ng-bind like so :

<p ng-bind="instructor.first_name instructor.last_name"></p>

Whenever I try this I get the following error:

Error: $parse:syntax Syntax Error

I know I can do the same using the curly braces

<p>{{instructor.first_name}}{{instructor.last_name}}</p>

but I would like to avoid this if I can since the rest of the code base uses ng-bind and I would to stay consistent. Thanks.

5
  • Docs say no Commented Jun 23, 2015 at 16:35
  • Haven't tried this, but I think you have to treat ng-bind values as expressions; so try instructor.first_name + " " + instructor.last_name Commented Jun 23, 2015 at 16:37
  • No it doesn't but I am just going to reformat the html a bit more with span tags inside the p tag and use ng-bind on each of those. Thanks Commented Jun 23, 2015 at 16:41
  • 1
    @ChadWatkins Here's an example of it working: plnkr.co/edit/ls0AkKUpY7z0xNeY0xB3?p=preview Commented Jun 23, 2015 at 16:55
  • @AndrewBurgess Awesome, thank you. I had double quotes on the outside and I think that stopped it from working. Commented Jun 23, 2015 at 17:05

3 Answers 3

34

You can use "+" for concatenation of the expressions. The following should work for you: <p ng-bind="(instructor.first_name) + (instructor.last_name)"></p>. You can even add filters there <p ng-bind="(instructor.first_name | filterName) + (instructor.last_name)"></p>.

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

Comments

7

You can always use ng-bind-template to bind and format multiple expressions. This is somewhat of a combination of your ng-bind and curly braces, but I think it's what you are looking for.

Your example would be:

<p ng-bind-template="{{instructor.first_name}} {{instructor.last_name}}"></p>

And of course there is also a ng-bind-html if you are looking to bind an html string.

Comments

2

Following the same idea of past answers you can also use ng-bind-html if you want to concat any other chars, that was my case:

  <td ng-bind-html="( com.ref.number | highlight: searchTerm) + '-' + (com.ref.order)">
  </td>

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.