如何在Ember Handlebars中不使用if语句实现?


Answers:


416

简单问题的简单答案:

{{#unless isValid}}
{{/unless}}

另外请记住,您可以{{else}}{{#if}}{{#unless}}与结束标记之间插入in 。


谢谢,我认为他们应该将其命名为{{#ifnot标志}},而不是{{#unless标志}}。
法比奥卡卡莫

2
@FabioCaccamo最初的实现者是Rubyist,其中unless的相当常见if
Christopher Swasey

35

您有很多方法可以做到这一点。

1.使用{{unless}}

{{#unless isValid}}
  ...
{{else}}
  ...
{{/unless}}

2.使用内联if助手:

{{#if (if isValid false true)}}
  ...
{{else}}
  ...
{{/if}}

3.使用ember-truth-helpers插件:

{{#if (not isValid)}}
  ...
{{else}}
  ...
{{/if}}

发现非常有用,可用于下一种方式:{{input type =“ text” ... disabled =(not someProperty)...}}
lesyk

5

它可以通过多种方式完成。

1次使用 unless

{{#unless IsValid}}
<Your Code>
{{/unless}}

2.使用 if else

{{#if IsValid}}
{{else}}
<Your Code>
{{/if}}

3.使用not助手

{{#if (not IsValid)}}
<Your Code>
{{/if}}

3

unless阻止助手(内置助手)

unless助手是助手的逆函数if

如果表达式返回假值,则将渲染其块。

  {{#unless valid}}
  <h3 class="warning">WARNING</h3>
  {{/unless}}

1
{{#if items.length}}
    //Render
{{/if}}

在这里items.length ..如果它返回除null以外的其他值,则只有它会进入if循环。

注意:您也可以检查布尔值。在If块中

{{#if booleanFloag}}

0

如果您要使用if和else,以下声明将对您有帮助:

{{#if author}}
    <h1>{{firstName}} {{lastName}}</h1>
{{else}}
    <h1>Unknown Author</h1>
{{/if}}

注意:在逻辑完成之前,请勿关闭if块。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.