Handlebars条件类属性

alt*_*alt 16 handlebars.js ember.js

使用EmberJS/Handlebars,只有条件为真时,如何才能向元素添加类?

<div {{#if isSearching}}class="foo"{{/if}}></div>
Run Code Online (Sandbox Code Playgroud)

像这样,但更少的伪代码和更多的现实.

Ada*_*per 51

不推荐使用 {{bind-attr}} .您可以在Ember 1.10及更高版本中使用新的更好的绑定属性语法,如下所示:

<div class="{{if isSearching 'foo'}}"></div>    
Run Code Online (Sandbox Code Playgroud)


小智 9

您必须使用{{bind-attr}}具有布尔条件的帮助程序,您可以阅读有关此内容的指南.

在您的情况下,如果isSearching属性位于控制器中,您可以执行以下操作:

<div {{bind-attr class="isSearching:foo"}}></div>
Run Code Online (Sandbox Code Playgroud)

你可以在这个小提琴中看到整个代码:http://jsfiddle.net/NQKvy/240/


cho*_*per 7

这不起作用,因为{{#if}}帮助程序将在HTML中创建标记.改为:

<div {{bind-attr class="isSearching:foo"}}></div>
Run Code Online (Sandbox Code Playgroud)

  • bindAttr 是“旧”的 Ember 语法顺便说一句(不用于更新的版本)。 (2认同)