Javascript 使用 ng-bind 绑定多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31008455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:03:32  来源:igfitidea点击:

bind multiple values using ng-bind

javascriptangularjsng-bind

提问by Chad Watkins

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

我可以像这样使用 ng-bind 绑定多个值吗:

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

Whenever I try this I get the following error:

每当我尝试这样做时,我都会收到以下错误:

Error: $parse:syntax Syntax Error

错误:$parse:syntax 语法错误

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.

但如果可以的话,我想避免这种情况,因为代码库的其余部分使用 ng-bind,我会保持一致。谢谢。

回答by Maksym Kosak

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

您可以使用“+”来连接表达式。以下应该对您有用:<p ng-bind="(instructor.first_name) + (instructor.last_name)"></p>. 您甚至可以在那里添加过滤器<p ng-bind="(instructor.first_name | filterName) + (instructor.last_name)"></p>

回答by djmarquette

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.

您始终可以使用 ng-bind-template 来绑定和格式化多个表达式。这有点像你的 ng-bind 和花括号的组合,但我认为这就是你正在寻找的。

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.

当然,如果您想绑定一个 html 字符串,还有一个 ng-bind-html。

回答by Juan Garassino

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

遵循过去答案的相同想法,ng-bind-html如果您想连接任何其他字符,也可以使用,这就是我的情况:

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