首页 > 解决方案 > 为什么我在 Handlebars 中存在范围问题?

问题描述

在我的 Handlebars 代码中,我在 if 块(或 ifCond 块)中有一个未定义的变量,但没有 if 块我有一个很好的结果。

这是我的代码:

{{#each array}}
   <select>
      <option {{#if variable}}{{else}} selected="selected"{{/if}}>-    </option>
      {{#for x}}
         <option {{#ifCond ../variable this}} selected="selected"{{/ifCond}} value="{{this}}">{{this}}% {{../variable}}</option>
      {{/for}}
   </select>
{{/each}}

在第 5 行的 % 之后,我有一个很好的结果。

感谢所有帮助!

标签: javascripthandlebars.js

解决方案


您可以使用反转 if 而不是使用空 if#unless

{{#unless variable}} selected="selected"{{/unless}}

推荐阅读