Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bind multiple values using ng-bind

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.

like image 420
Chad Watkins Avatar asked Jun 23 '15 16:06

Chad Watkins


People also ask

Can Ngmodel have multiple values?

The solution is ng-bind-template, it can bind more than one {{}} expression, so it can show more than a single value that was declared in the Script function.

What does ng bind do?

The ng-bind directive tells AngularJS to replace the content of an HTML element with the value of a given variable, or expression. If the value of the given variable, or expression, changes, the content of the specified HTML element will be changed as well.

How can you bind an element to data in AngularJS?

AngularJS creates a two way data-binding between the select element and the $ctrl. orderProp model. $ctrl. orderProp is then used as the input for the orderBy filter.


3 Answers

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>.

like image 110
Maksym Kosak Avatar answered Oct 06 '22 00:10

Maksym Kosak


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.

like image 24
djmarquette Avatar answered Oct 06 '22 01:10

djmarquette


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>
like image 42
Juan Garassino Avatar answered Oct 06 '22 00:10

Juan Garassino